Future Mocking allows you to mock members without passing the dependency through a constructor or calling a method.
You can apply future mocking automatically based on an expectation rather than applying it to every instance explicitly.
You will find this handy especially in case you have to mock third party controls and tools where you have little control
over the way in which they are created.
Ignore instance for an expectation explicitly
By default all mocks are tied to an instance and this is the expected behavior. For instance
in the following sample we have created an object of type UserData
which has a single method called ReturnFive() which returns an
integer. Then we mock the call to the ReturnFive()
method for the fakeUsed instance of the UserData
class. But note that the ReturnFive() method will not
be mocked for any of the other instances of the UserData class.
If we want to apply future mocking and assure that all UserData instances use the same mocked version of the
ReturnFive() method, we can call IgnoreInstance().
When IgnoreInstance is applied, the ReturnFive() method will
work according to the expectation you have set in Mock.Arrange.
When setting Future expectations you can also use the Fluent Mocking API as in this example:
Using IgnoreInstance for virtual methods
You can also apply IgnoreInstance to virtual methods. Imagine that we have a simple class
Calculus which has a virtual method Sum:
Now we can use IgnoreInctance in the exact same way:
Using IgnoreInstance for Collections
There is an example where we want to Arrange a property to return fake collection.
To show you this we will use the Foo class below.
We will also use a public method that returns our fake collection.
This is how the test will look like:
You can see that the test logic is the same as in the previous tests with the only difference that you are returning a collection this time.
Future Constructor Mocking
With Telerik JustMock you are even able to future mock the constructor of an instance.
Let`s assume we have the following class:
We can easily arrange its constructor like this:
This will apply DoNothing to the constructor of every new instance of type
FooWithNotImplementedConstructor called during the test method.
Mocking the new operator
With Telerik JustMock you can now arrange a return value for a new object creation.
Let's assume we have the following class:
We can easily arrange each new instance of the FooWithProp class, to return a predefined object of the same type:
This will work for each new instance of the FooWithProp type outside the test method.
Still, it applies only for code, executed under the test context.
Future mocking across threads
Future mocking across all threads is an unsafe operation because it may compromise the
stability of the testing framework. Arrangements that ignore the instance are valid only for the current thread by default.
To make an arrangement that ignores the instance valid on all threads, add the .OnAllThreads() clause to the arrangement:
Mock.Arrange(() => new DBContext()).DoNothing().OnAllThreads();
Let's see a UI example where we have a form.
Based on some action against the form, it raises an event which needs to be handled in a specific way therefore in the unit test
we want to assert the expected value of the handler execution.
this.service = new EntryService();
service.Saved += new EventHandler<EntrySavedEventArgs>(service_Saved);
Imagine that we have defined an EntryService which purpose is to save some
entries to a database when the user has made any changes.
For instance we can have a button on the form and when the user is finished editing the entries
pressing this button will trigger a call to the following method:
public void SaveToDatabase(string value)
catch (DuplicateEntryException ex)
MessageBox.Show("Entry Duplicated " + ex.DuplicatedValue);
Here is the handler for the service.Saved event:
public void service_Saved(object sender, EntrySavedEventArgs e)
this.label1.Text = "Saved string : " + e.EntryValue;
Next is a simple test using
where we have created an event handler which will receive as an argument the value which was passed to the
service.Save call in the SaveToDatabase method.
public class when_save_to_database_is_invoked_on_form
Establish context = () =>
IEntryService serviceMock = Mock.Create<EntryService>();
Mock.Arrange(() => serviceMock.Save(valueToSave)).Raises(() => serviceMock.Saved += null, new EntrySavedEventArgs(valueToSave));
private Because of = () =>
sut = new Form();
private It should_assert_that_label_contains_expected_valueToSave = () =>
sut.label1.Text.ShouldEqual("Saved string : " + valueToSave);
static Form sut;
const string valueToSave = "Raise Event";
Here we can see that although no instance is supplied to the target UI class, JustMock picks up the intended setup from the context.