Read More on Telerik Blogs
May 12, 2015 Release
Get A Free Trial

JustMock for Devices is a version of JustMock that allows you to use the familiar JustMock syntax to create, arrange and use mocks in tests that run on devices. It supports Windows Phone and Xamarin Android/iOS, and works both in the emulator/simulator and on the devices. You can use it both with Visual Studio on Windows and with Xamarin Studio on Mac.

The feature set of JustMock for Devices is similar to that of JustMock Lite–you can mock interfaces, virtual members on classes that can be sub-classed and delegates. All arrangement and assertion features are supported except a few exotic ones (fore example, Wait.For and SetInterceptionFilter).

The package will be delivered through the JustMock and JustMock Trial installers. Now it is available in the latest internal build of JustMock in a Beta stage.

Installation in all cases is done through the NuGet package manager, but you need to install JustMock first.

Installation and Setup in Visual Studio

Step 0

The internal build installer may not add the NuGet package source for JustMock, so you may need to add it manually. Skip this step if the JustMock package source is already present in the list of package sources.

Open Tools menu -> Options -> NuGet Package Manager -> Package Sources

  • Hit the + button
  • Enter some Name (“JustMock IB” is a good choice)
  • Enter “C:\Program Files (x86)\Telerik\JustMock\Libraries\NuGet” as Source
  • Hit “Update”

The dialog should look like it does in the screenshot above.

Step 1

So, you have a Windows Phone Unit Test project and you want to use JustMock for mocking.

Right-click on the References node of your unit test project in the Solution Explorer and select "Manage NuGet Packages." Select the "JustMock IB" package source under the "Online" section.

There are two packages you can install.

  • JustMock for Devices: This is the core JustMock package that brings you the power of JustMock Lite to run tests on devices.
  • JustMock Auto-Mocking for Devices: This package provides a mocking container (based on Autofac) in addition to the core JustMock package. Installing this package will also bring in Autofac into your unit test project.

Install either of these packages to continue. If you’re not sure what to use, install JustMock Auto-Mocking for Devices for the full experience.

Once installed, your project should look something like this:

You will get a reference to Telerik.JustMock.Portable and, optionally, to Autofac and Telerik.JustMock.Portable.Autofac. You will also get a file named FodyWeavers.xml. This file is part of the JustMock machinery and should be committed as part of your project.

Step 2

You’re ready to do some mocking. Here’s a silly test I wrote:

[TestMethod]
public void MockInterface()
{
    var serviceProvider = Mock.Create<IServiceProvider>();
    Mock.Arrange(() => serviceProvider.GetService(typeof(IServiceProvider)))
        .Returns(serviceProvider);
   
    Assert.AreSame(serviceProvider,
        serviceProvider.GetService(typeof(IServiceProvider)));
}

Select the emulator:

And run the tests:

All is green!:

If you check the build log, you’ll see something along the following lines:

1>------ Build started: Project: UnitTestApp3, Configuration: Debug x86 ------
1>    Fody (version 1.28.3.0) Executing
1>      Finished Fody 598ms.
1>  UnitTestApp3 -> C:\Users\dragnev\documents\visual studio 2013\Projects\UnitTestApp3\UnitTestApp3\bin\x86\Debug\UnitTestApp3.dll
1>      Skipped Verifying assembly since it is disabled in configuration
1>      Finished verification in 3ms.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Notice that there are a few lines mentioning Fody–this is part of the special sauce that enables the creation of mocks on devices, where you can’t execute dynamically generated code.

Installation and Setup in Xamarin Studio on Mac

The installation process in Xamarin Studio is about the same as the one for Visual Studio, because both are based on NuGet. Macs, however, cannot execute PowerShell scripts, so you will need one additional simple step to get JustMock up and running.

Step 1

Copy the JustMock NuGet packages to a new folder on your Mac (in a Windows installation they can be found in “C:\Program Files (x86)\Telerik\JustMock\Libraries\NuGet”). Configure a new NuGet Package Source to point to that folder:

Step 2:

Select the JustMock package source. The JustMock packages should pop up in the list:

Refer to the Visual Studio section, if you’re wondering which package to install.

After installing the package of your choice, you will get a warning, which is normal and should be disregarded:

Your project should now look something like this:

Step 3

Just a few manual setup things before we’re ready to mock.

Delete the Fody_ToBeDeleted.txt file.

Open the FodyWeavers.xml file and paste the following settings in:

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
  <JustMock/>
</Weavers>

 

Step 4

You’re ready to mock! Here’s another silly test:

[Test] 
public void MockThing() 
    var mock = Mock.Create<IUIKeyInput> (); 
    Mock.Arrange (() => mock.HesText).Returns (true); 
    Assert.IsTrue (mock.HesText); 
}

 

Select an iOS Simulator and hit “Run.”

The simulator will boot up and you’ll be able to run the test:

Feedback

JustMock for Devices is still in Beta and we welcome all sorts of feedback that will allow us to stabilize and improve it. If you run into any issues, please open a ticket describing the problem, your setup and anything else that will help us reproduce the issue.

Happy mocking!
The Just* Team


About the Author

Stefan Dragnev

Stefan Dragnev is a Principal Software Engineer at Telerik. 

Related Posts