MC
Metalama CommunityXaniff
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?
Xaniff•258d ago
Using xUnit 2.5.1 and .net7.0
domsinclair•257d ago
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.
Petr Onderka•257d ago
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.)
Xaniff•257d ago
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:
Petr Onderka•257d ago
Do you see the same exception if you remove the
ProjectReference
? (And do whatever you need to make the project compile without that.)Xaniff•257d ago
The project appears to compile without an issue - I'm just getting that error in the test runner
Xaniff•257d ago
Petr Onderka•257d ago
I understand that. But I assume it won't compile if you remove the
ProjectReference
.Xaniff•257d ago
Probably not since that's where the aspect lives
Xaniff•257d ago
Xaniff•257d ago
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
Petr Onderka•257d ago
I'm still unable to reproduce this. Can you share the whole solution that contains the two projects?
Xaniff•257d ago
Yeah, give me a few
It's part of a much larger solution, so let me extract it out
Petr Onderka•257d ago
Thanks.
Xaniff•257d ago
What's your email? I'll invite you to the project.
@petronderka
Found you - added on github
Petr Onderka•257d ago
Thanks, I get the exception now and will investigate what's causing it.
Xaniff•257d ago
Awesome, thank you
Petr Onderka•257d ago
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.Xaniff•257d ago
Curious, but I'll give that a shot
Xaniff•257d ago
I'm still seeing the same exception at test-time in the command prompt:
Petr Onderka•257d ago
Odd, that's not what I see. Anyway, I'll keep looking.
Xaniff•257d ago
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?
Petr Onderka•257d ago
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.
Xaniff•257d ago
Well, I'll just disable the test project until you're able to produce another preview build with a fix then
Petr Onderka•257d ago
I have a fix for this that should be released in the 2023.3 RC release, which is planned for this week.
Xaniff•257d ago
Very exciting - thank you
Xaniff•248d ago
@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...
Xaniff•248d ago
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...
Gael Fraiteur•248d ago
Yes it was released.
Xaniff•248d ago
Excellent, thank you
Gael Fraiteur•248d ago
I see we didn't solve tickets after the release but in this case there is no ticket.
Xaniff•248d ago
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
Gael Fraiteur•248d ago
Yes because Discord/Slack are not integrated with our bug tracking system. GitHub is.
Want results from more Discord servers?
More PostsHow to debug a "Cannot preview the transformed code: unknown error"?I've got an aspect that's building without error and a target project that's applying the aspect thaWarn on ineligibility instead of throwingI've got two conditions in my BuildEligibility for a method override aspect: If the target type is aReferencing value assigned to variable in statement builder within another statementGiven a statement that does something like:
```cs
var expr = new ExpressionBuilder();
expr.AppendVerSpecifying generic constraints of compile-time introduced typeI have a TypeAspect that has three attribute arguments when applied to a type. One of these is a `TyStep-through aspect-debuggingI'm sure I've asked this before, but I cannot find the thread in which it was answered (if I actuallCodeLens HelpMetalama is configured in CodeLens, and I have an aspect that has been applied. I am able to right-How to apply an attribute to a method introduced by another aspect?I have an aspect that has a dependency on another aspect. However, I'd like for downstream projects Advising the addition of code commentsUsing Metalama
Is there a way to advise adding code comments? For example, when I advise a method Set eligibility to not run against (built-in) methods of record classes or structsI've figured out how to set up an `If` statement for the record types, but I cannot figure out how tHow to specify an IMethod as the callback for an event?I'd like to specify an introduced method on a TypeAspect as the callback for an event handler.
1) WCreate NuGet with MetalamaWith source generators I have to add some MSBuild configuration, but with Metalama it seems I don't Override property with BuildAspectI create exceptions inheriting from Exception and putting the `[BusinessException]` attribute.
I waView diff in RiderI don't use Visual Studio, and overall, I don't have Windows. Is there any way to see the source genOverride constructorIs it possible to override a constructor?
I want to take my code:
```
public class BusinessExceptiDoes Metalama only support attributes?I navigated in all Metalama documentation, but I want to override a method for all types that inheriCan Metalama suppress compiler errors?Hello! I want to know if I can use Metalama in my library.
The users of my library have to declare Eligibility rule based on target method not returning voidTypically eligibility rules I've put together are based on a specifically typed return or paramters.Follow up question from yesterday's meetupNow that I've had time to review what was discussed I think I can now ask the question I'd wanted toPlease cache licensing key rather than check every buildEvery time I attempt to build an aspect today, I'm getting an error that I've got an invalid licenseDocumentation: Bad linkAt https://doc.metalama.net/conceptual/using/fabrics there's a link at the bottom titled "Configurin