This is a migrated thread and some comments may be shown as answers.

Mocking System.Windows.Media.Color

11 Answers 140 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
SelAromDotNet
Top achievements
Rank 2
SelAromDotNet asked on 01 Sep 2013, 08:32 PM
I'm attempting to test an app that creates an instance of the struct System.Windows.Media.Color.

The color is loaded fine in the actual app, but when testing, I get the error: System.InvalidProgramException: Common Language Runtime detected an invalid program.

This happens on the line where the color is created using Color.FromArgb()..

So I thought perhaps I need to mock the struct and static method. This is what I tried:

var color = Mock.Create<Color>(Constructor.Mocked, Behavior.Loose);
Mock.Arrange(() => Color.FromArgb(Arg.IsAny<byte>(), Arg.IsAny<byte>(), Arg.IsAny<byte>(), Arg.IsAny<byte>())).IgnoreArguments().IgnoreInstance().Returns(color);

but when I run this, I get this error: Telerik.JustMock.MockException: Implementation of method Equals in value types must not throw for mocked instances.

Is there something additional I need to do to mock this struct?

11 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 02 Sep 2013, 06:36 AM
Hi Josh,

>>> I get the error: System.InvalidProgramException: Common Language Runtime detected an invalid program.

Could you give me the snippet that causes this exception? I'd like to test it locally.

>>> So I thought perhaps I need to mock the struct and static method.

Creating a loose mock for a struct is seldom a good idea. Since value types don't have an identity (essentially ReferenceEquals doesn't work for them), JustMock relies on a working implementation of a.Equals(b) in the struct to tell it whether a given struct is a mock or not. There are value types whose Equals method unfortunately throws exceptions or simply doesn't work when applied to a loose mock so it is inherently impossible to for JustMock to create loose mocks for those value types. Fortunately, that's rarely a problem, because value type instance method mocking is rarely useful - as I believe is your case as well.

That System.InvalidProgramException you're getting however sounds like a true bug. I'd like to tackle that here.

Send me a repro snippet and we'll work from there.

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
SelAromDotNet
Top achievements
Rank 2
answered on 04 Sep 2013, 05:04 AM
Well, the line that is failing is this one:

team1.TeamColor = Color.FromArgb(255, 200, 200, 0);

Where team1 is just a POCO class with a System.Windows.Media.Color property called TeamColor. When I run the actual program this executes without error.

But when running the test this line fails, here's the full stack trace from the test runner:

Test Name:  EndLastRoundEndsGame
Test FullName:  GameTests.EndLastRoundEndsGame
Test Source:    \GameTests.cs : line 156
Test Outcome:   Failed
Test Duration:  0:00:01.4459321
 
Result Message:
Test method GameTests.EndLastRoundEndsGame threw exception: 
System.InvalidProgramException: Common Language Runtime detected an invalid program.
Result StackTrace: 
at System.Windows.Media.Color.FromArgb(Byte a, Byte r, Byte g, Byte b)
   at Data.Game..ctor() in \Game.cs:line 54
   at Data.Game..ctor(Int32 numCards) in \Data\Game.cs:line 68
   at Test.GameTests.MockFirstRound() in Test\GameTests.cs:line 183
   at Test.GameTests.EndLastRoundEndsGame() in Test\GameTests.cs:line 158

Please let me know if I can provide any more information. It's starting to look like this isn't a bug with JustMock as much as it is with the test runner, so if you do discover anything helpful I'd greatly appreciate it, as this is causing ALL my tests to fail...

thanks,
josh
0
Stefan
Telerik team
answered on 04 Sep 2013, 08:24 AM
Hello Josh,

I couldn't reproduce this behavior on my machine. Do you have the JustMock profiler enabled? If yes, could you try running this test without the profiler enabled? It is possible that the profiler corrupts some method which then manifests in an InvalidProgramException.

Since I wasn't able to reproduce the issue, could you create a repro project that demonstrates this behavior and send it to me?

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
SelAromDotNet
Top achievements
Rank 2
answered on 04 Sep 2013, 03:33 PM
I disabled the profiler and the error still exists which tells me it has to be a problem with either my project or the test project, not JustMock!

Sorry for that, I'll see if I can get more info and report back if I find a fix...
0
Stefan
Telerik team
answered on 05 Sep 2013, 06:12 AM
Hello Josh,

I suggest you also check if there are other profilers trying to attach. You can do this by opening the Event Log, navigating to Windows Logs->Application and see if there are any events with ID 1022 getting added whenever you start the test runner.

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
SelAromDotNet
Top achievements
Rank 2
answered on 05 Sep 2013, 03:01 PM
After spending many hours looking into this, it appears that the problem is my attempt to reference the System.Windows.Media.Color property at all in my test project, specifically for testing.

I suspect that using this property is intended for the UI thread for whatever reason, so any attempts to use it without a UI context are failing.

In other words, this feels like a bug in the TEST tool of Visual Studio more than anything else.

I'm no expert on testing, VS, or XAML so I could be wrong, but in the end I worked around this by storing the color as a string, then using a converter in XAML to turn it into a color in the frontend.

This is sufficient for me to move on, HOWEVER, I do feel that if I could have mocked the struct, I might have been able to work around it without having to change my code, what do you think?

 either way thanks for your input! 
0
Stefan
Telerik team
answered on 06 Sep 2013, 05:08 AM
Hello Josh,

I had no problem referencing and using the System.Windows.Media.Color class in both my .NET 4 MSTest project and my .NET 3.5 NUnit project. And even if there were a problem related to there not being a UI thread, the InvalidProgramException is not the exception you'd have gotten. InvalidProgramException is usually thrown when the CLR tries to JIT-compile a method from an invalid assembly - one with broken IL code, or with IL code using broken metadata.

Since it's nearly impossible that a .NET Framework assembly is corrupt, I suggested looking for more plausible explanations. One such explanation is that a different profiler-based tool is active (e.g. some code coverage tool or profiler). Another explanation is that your actual reference is subtly broken and doesn't point straight to the framework assembly. Maybe you tried using Microsoft Fakes to make shims for the PresentationCore assembly? Maybe you're referencing the wrong version of the PresentationCore assembly? You could try opening your project file in a text editor and visually inspecting your assembly references and see if they look completely normal.

As a general precaution, I'd suggest you keep looking for the underlying cause, instead of making workarounds, because in the future this issue might come back and bite you again.

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
SelAromDotNet
Top achievements
Rank 2
answered on 06 Sep 2013, 06:12 PM
is your main project a Windows Phone project specifically (not just regular .net 4)? If so can you send it to me? I'd love to see how you set it up... maybe I can see what I did differently...
0
Stefan
Telerik team
answered on 09 Sep 2013, 09:04 AM
Hello Josh,

No, we don't have Windows Phone projects. Can you describe your project structure in more detail?

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
SelAromDotNet
Top achievements
Rank 2
answered on 10 Sep 2013, 03:48 PM
I simply have a Windows Phone application, which has a class that stores System.Windows.Media.Color as a property, and in its constructor, assigns a color to that property.

In the test project, I create a new instance of that object and I get the exception shown previously. That's really the main bits at work I think... My assumption is that the MSTEST doesn't support Windows Phone fully yet...

If I could Mock this value that might be a nice way to workaround it, but I don't know enough about it to say for sure...

anyway let me know if you need more detail. I can't send you the project because I already changed it to use a string instead of the Color property.
0
Deyan
Telerik team
answered on 11 Sep 2013, 10:22 AM
Hello Josh,

To our knowledge, MSTEST may not fully support Windows Phone. There are a couple of articles on the Internet that give instructions on how MSTEST is used with Windows Phone. Telerik's internal tests of the Windows Phone components are not based on MSTEST, but on the Windows Phone Unit Testing framework. Note that this framework is incompatible with JustMock.

Regards,
Deyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
SelAromDotNet
Top achievements
Rank 2
Answers by
Stefan
Telerik team
SelAromDotNet
Top achievements
Rank 2
Deyan
Telerik team
Share this question
or