it's time to do some unit testing with MVVM Light ViewModels.
Setup:
- Visual Studio 2010 SP 1
- Windows Phone 7.1 SDK Release Candidate
- Silverlight NUnit Project -template
- Moq (4.0.10827 Final)
Steps:
- Create a new MvvmLight (WP7) -project
- Convert project to WP7.1
- Create a new Silverlight NUnit -project
- Reference WP7-project to Silverlight NUnit -project
- Add a dummy method to the MainViewModel (f.ex. public string DoSomething())
- Add a test that instantiates MainViewModel, calls the dummy method and asserts.
- Run tests -> everything should work as expected
- Add reference to Moq
Add second test method with some Moq. I simply copy-pasted this demo code from Moq's site:
var mock = new Mock();
// WOW! No record/replay weirdness?! :) mock.Setup(framework => framework.DownloadExists("2.0.0.0")) .Returns(true) .AtMostOnce();
// Hand mock.Object as a collaborator and exercise it, // like calling methods on it... ILoveThisFramework lovable = mock.Object; bool download = lovable.DownloadExists("2.0.0.0");
// Verify that the given method was indeed called with the expected value mock.Verify(framework => framewo开发者_JAVA技巧rk.DownloadExists("2.0.0.0"));
- Run tests.
This is what i get via NUnit runner at step 9:
SilverlightNUnitProject2.SilverlightTests.TestSomething:
System.TypeLoadException : Could not load type 'System.Action' from assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Question:
What is going on here? Which part of my setup is failing and why?
Update!
I found a blog post related to this problem here. This inspired me to download and try the exactly same version of Moq (3.1.416.3) used in that article. And whaddayouknow? It works.
I'm not going to put this up as an answer, because i still don't know what's going on here. Original question still stands, i think.
精彩评论