2

I am trying to set up some tests for some existing code and need to be able to mock things like ViewFlipper.FindViewById.

So far to do this I have had to create a mockable wrapper for the ViewFlipper. Now the issue I have is that I want to return a mocked view. The mocked view needs to have a Context passed into it's ctor. I have found the MockableContext class in the Java.Test namespace but it doesn't seem to like being newed up? I get null ref exceptions?

Should I be able to use this class in this manner? Is there another way to do this? Simply creating a new Mock<Context> doesn't work either :/

poupou
  • 43,413
  • 6
  • 77
  • 174

2 Answers2

0

Have you tried using Android.App.Application.Context property which I believe is equivalent to the Android.Content.Context.ApplicationContext property, except that it's static and available everywhere.

I came across it in this Stack Overflow answer here:

https://stackoverflow.com/a/19248475/1071320

Community
  • 1
  • 1
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
0

The issue is due to the test units running as a .NET assembly which simply does not exist within the "Context" (pun intended), of Android.

Examination of Android.App.Application.Context will result in the same exception.

Android.Test.Mock.MockContext is a wrapper around Java code and when running a unit test, the assembly is running in a Windows environment, not a Java environment.

It would be nice to be able to unit test a Xamarin view... but I am uncertain that it is possible to mock the Android context that is required.

WindSpirit
  • 181
  • 1
  • 7