Unable to get aspect testing to work
I've got a very simple class set up to apply a logging aspect to so I can get the hang down of testing aspects. I've created the starting class to test with Metalama.Framework (latest preview) installed, viewed the preview to get the aspect-applied version and copied that into <name>.t.cs and then installed the Metalama.Testing.AspectTesting package (latest preview).
When I run all tests, I get the following (per the attached image).
Any guesses what I'm doing wrong here?
33 Replies
Using xUnit 2.5.1 and .net7.0
I find it easier to set the test project up (and include the metalama testing nuget first (but realistically that shouldn't make too much of a difference when you add it , just as long as you do).
However where you source your 'compiled' code from will make a difference. If you haven't watched it yet then I recomend looking at this video ( https://doc.metalama.net/videos/testing ). Note the recomendation to run the test first so that it fails and then use the code that the failed test message expects to see as the 'compiled' code that you add to the .t.cs file.
Lastly it might pay you to go back to xUnit 2.5. Not sure if 2.51 has been tested yet with metalama.
Lastly it might pay you to go back to xUnit 2.5. Not sure if 2.51 has been tested yet with metalama.
I was not able to reproduce this using xunit 2.5.1-pre.26. Can you share a project that reproduces the issue? (I suspect it's something in the .csproj.)
Here's my csproj:
Tried removing the .t.cs file and still see the same error:
System.InvalidOperationException : The test assembly must have an AssemblyMetadataAttribute with Key = "ProjectDirectory".
Tried updating the csproj to more accurately reflect the sample given in the docs, but same error:
Do you see the same exception if you remove the
ProjectReference
? (And do whatever you need to make the project compile without that.)The project appears to compile without an issue - I'm just getting that error in the test runner
I understand that. But I assume it won't compile if you remove the
ProjectReference
.Probably not since that's where the aspect lives
I've watched about half the video about it so far and I don't see that I've done anything differently here absent updating the nuget packages from the template
But here he shows the invalid test, I get this error
I'm still unable to reproduce this. Can you share the whole solution that contains the two projects?
Yeah, give me a few
It's part of a much larger solution, so let me extract it out
Thanks.
What's your email? I'll invite you to the project.
@petronderka
Found you - added on github
Thanks, I get the exception now and will investigate what's causing it.
Awesome, thank you
I still don't understand the problem, but it seems a workaround is to run
dotnet test
from the command line once. After that, it seems running the tests from VS works.Curious, but I'll give that a shot
I'm still seeing the same exception at test-time in the command prompt:
Odd, that's not what I see. Anyway, I'll keep looking.
I appreciate it!
I wonder if the issue isn't with the fact that this aspect is making use of the dependency injection feature - as it's a standalone unit test with no entry point, there's no opportunity to register DI.
At compile time, it wouldn't care - it'd set it up as a field and in the constructor as it would, but at runtime there would be an attempted injection that wouldn't actually happen. I don't know what magic the Metalama Aspect Testing package is trying to do under the hood, but perhaps it's something related to this?
I don't think so. It seems to be caused by our MSBuild customization. If build events happen in a different order, it doesn't generate assembly attributes that are needed by the test framework.
Well, I'll just disable the test project until you're able to produce another preview build with a fix then
I have a fix for this that should be released in the 2023.3 RC release, which is planned for this week.
Very exciting - thank you
@petronderka I don't see anything about this in the release notes (https://github.com/orgs/postsharp/discussions/218) for 2023.3.5-rc - did it make it in?
GitHub
Metalama 2023.3.5-rc released · postsharp · Discussion #218
We've just released Metalama 2023.2.5-rc which includes all fixes and enhancements from Metalama 2023.1.12 and Metalama 2023.2.5 in addition to following changes. New You can now use the C# Dev...
GitHub
Metalama 2023.4.1-preview released · postsharp · Discussion #219
We've just released Metalama 2023.4.1-rc which includes all fixes and enhancements from Metalama 2023.1.12, Metalama 2023.2.5, and 2023.3.5-rc, in addition to following changes. Fixed Fixed: Te...
Yes it was released.
Excellent, thank you
I see we didn't solve tickets after the release but in this case there is no ticket.
Yes, that's right. Once he was able to repro it himself, I didn't see a need to file a separate issue in Github as well
Yes because Discord/Slack are not integrated with our bug tracking system. GitHub is.