Question

I have an android unit test project which uses NUnit to test my Xamarin Android project and need to mock a shared preferences object. I have attempted to use the following to mock an ISharedPreferences:

new MockContext().GetSharedPreferences("",Android.Content.FileCreationMode.Append);

However this results in the following exception:

java.lang.ClassNotFoundException: Didn't find class "android.test.mock.MockContext"

I have included the following using statement in my project using Android.Test.Mock.

I do not know why it cannot find the class when the code compiles and builds successfully in Xamarin?

Était-ce utile?

La solution

It turns out that most of these class are not yet supported by Xamarin yet. Some other users have alluded to this in previous posts as well: Testing Activities in Xamarin.Android

Unfortunately there is no solution to mocking as of yet with Xamarin. See my other question at Mocking framework for use with xamarin android. For now we will just have to live with using manual mocking.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top