Fakes.Contrib 0.8.18200.2

This library is a contribution to Microsoft Fakes. It allows tests to verify expectations on mocks with an easy and fluent syntax.

Install-Package Fakes.Contrib -Version 0.8.18200.2
dotnet add package Fakes.Contrib --version 0.8.18200.2
paket add Fakes.Contrib --version 0.8.18200.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Fakes.Contrib

This library is a contribution to Microsoft Fakes. It allows tests to verify expectations on mocks with an easy and fluent syntax.

VSTS Build Status
SonarCloud QualityGate

Installation

To install Fakes.Contrib, run the following command in the Package Manager Console

PM> Install-Package Fakes.Contrib

Usage

Note: each scenarios use a stub generated from the interface below and using the Microsoft Fakes tooling.

public interface IMyComponent
{
	void MyMethod();
	
	void MyMethod(MyClass obj);
	void MyMethodOnMultiple(IEnumerable<MyClass> items);
	
	void MyOtherMethod(MyOtherClass obj);
	void MyOtherMethodOnMultiple(IEnumerable<MyOtherClass> items);
	void MyMethodUsingAnInterface(IMyOtherInterface obj);
}

Scenario 1: we want to verify that our SUT calls MyMethod() on the injected component.

// Arrange
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething();

// Assert
stub.AssertWasCalled(mock => mock.MyMethod());

Scenario 2: we want to verify that our SUT calls MyMethod() on the injected component and passes the reference to the object that it received.

// Arrange
var obj = new MyClass();
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyMethod(obj));

Scenario 3: we want to verify that our SUT calls MyMethod() on the injected component and passes any instance of the object's type.

// Arrange
var obj = new MyClass();
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyMethod(With.Any<MyClass>()));

Scenario 4: we want to verify that our SUT calls MyMethod() on the injected component and passes an instance that matches (using a predicate) the instance it received.

// Arrange
var obj = new MyClass { MyProperty = "Hello world !" };
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyMethod(
	With<MyClass>.Like(x => x.MyProperty.Contains("world"))));

Scenario 5: we want to verify that our SUT calls MyOtherMethod() on the injected component and passes an instance of a different type that matches (using a predicate) the instance it received.

// Arrange
var obj = new MyClass { MyProperty = "Hello world !" };
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomethingDifferent(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyOtherMethod(
	With<MyOtherClass>.Like(other => other.MyProperty == obj.MyProperty)));

Scenario 6: we want to verity that our SUT calls MyMethodOnMultiple() on the injected component and passes an IEnumerable of the references to the object that it received.

// Arrange
var array = new[]
{
	new MyClass { MyProperty = "Value 1" },
	new MyClass { MyProperty = "Value 2" }
};
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomehtingOnMultiple(array);

// Assert
stub.AssertWasCalled(mock => mock.MyMethodOnMultiple(
	With.Enumerable(array).Like<MyClass>((source, item) => source == item)));

Scenario 7: we want to verity that our SUT calls MyOtherMethodOnMultiple() on the injected component and passes an IEnumerable of different type that match (using a predicate) the IEnumerable it received.

// Arrange
var array = new[]
{
	new MyClass { MyProperty = "Value 1" },
	new MyClass { MyProperty = "Value 2" }
};
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomehtingDifferentOnMultiple(array);

// Assert
stub.AssertWasCalled(mock => mock.MyOtherMethodOnMultiple(
	With.Enumerable(array)
		.Like<MyOtherClass>((source, item) => source.MyProperty == item.MyProperty)));

Scenario 8: we want to verify that our SUT does not call MyOtherMethodOnMultiple() on the injected component.

// Arrange
var array = new MyClass[0];
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomehtingDifferentOnMultiple(array);

// Assert
stub.AssertWasNotCalled(mock => mock.MyOtherMethodOnMultiple(
	With.Enumerable(array)
		.Like<MyOtherClass>((source, item) => source.MyProperty == item.MyProperty)));

Scenario 9: we want to verify that our SUT does call a static property on a .NET component that was shimed.

using (var context = ObservableShimsContext.Create())
{
	// Arrange
	var now = new DateTime(2013, 1, 1);
	ShimDateTime.NowGet = () => now;
	var sut = MakeSut();
	
	// Act
	sut.ReturnDateTimeNow();
	
	// Assert
	context.AssertWasCalled(() => DateTime.Now);
}

Scenario 10: we want to verify that our SUT does call a static method on a .NET component that was shimed.

using (var context = ObservableShimsContext.Create())
{
	// Arrange
	ShimGuid.NewGuid = () => new Guid("97f31de6-5a03-454a-976f-76e2a54a89d4");
	var sut = MakeSut();
	
	// Act
	sut.ReturnNewGuid();
	
	// Assert
	context.AssertWasCalled(() => Guid.NewGuid());
}

Scenario 11: we want to verify that our SUT does call a method on an instantiated .NET component that was shimed. Note that with the current implementation of the shims' detour, the parameter cannot be verified.

using (var context = ObservableShimsContext.Create())
{
    // Arrange
    const string url = "http://www.dev-one.com";
    var sut = MakeSut();
    ShimHttpClient.AllInstances.GetStringAsyncString = (c, u) => Task.FromResult("ok");

    // Act
    await sut.GoOnTheWeb(url);

    // Assert (ATM, parameter cannot be verified)
    context.AssertWasCalled((HttpClient client) => client.GetStringAsync(url));
}

Release notes

  • Version 0.8:
    • added SourceLink support
    • minor code cleanup based on static analysis
    • adding support for shims with void returns
  • Version 0.7:
    • added support for interface types
    • removed assembly signing
    • updated copyright and maintainer/owner information
  • Version 0.6: added an ObservableShimsContext to allow verifications on shims
  • Version 0.5: added an AssertWasNotCalled assertion
  • Version 0.4: improved With to allow predicate of two different types
  • Version 0.3: added more flexible ways to verify a mock
  • Version 0.2: first usable version. Watch out, this is still apha code !

About the authors

This package is maintained by John M. Wright [ Twitter: @Wright2Tweet ]

Fakes.Contrib was originally written and maintained by Fabian Vilers and Dev One. [ Twitter: @fvilers ]

Fakes.Contrib

This library is a contribution to Microsoft Fakes. It allows tests to verify expectations on mocks with an easy and fluent syntax.

VSTS Build Status
SonarCloud QualityGate

Installation

To install Fakes.Contrib, run the following command in the Package Manager Console

PM> Install-Package Fakes.Contrib

Usage

Note: each scenarios use a stub generated from the interface below and using the Microsoft Fakes tooling.

public interface IMyComponent
{
	void MyMethod();
	
	void MyMethod(MyClass obj);
	void MyMethodOnMultiple(IEnumerable<MyClass> items);
	
	void MyOtherMethod(MyOtherClass obj);
	void MyOtherMethodOnMultiple(IEnumerable<MyOtherClass> items);
	void MyMethodUsingAnInterface(IMyOtherInterface obj);
}

Scenario 1: we want to verify that our SUT calls MyMethod() on the injected component.

// Arrange
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething();

// Assert
stub.AssertWasCalled(mock => mock.MyMethod());

Scenario 2: we want to verify that our SUT calls MyMethod() on the injected component and passes the reference to the object that it received.

// Arrange
var obj = new MyClass();
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyMethod(obj));

Scenario 3: we want to verify that our SUT calls MyMethod() on the injected component and passes any instance of the object's type.

// Arrange
var obj = new MyClass();
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyMethod(With.Any<MyClass>()));

Scenario 4: we want to verify that our SUT calls MyMethod() on the injected component and passes an instance that matches (using a predicate) the instance it received.

// Arrange
var obj = new MyClass { MyProperty = "Hello world !" };
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomething(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyMethod(
	With<MyClass>.Like(x => x.MyProperty.Contains("world"))));

Scenario 5: we want to verify that our SUT calls MyOtherMethod() on the injected component and passes an instance of a different type that matches (using a predicate) the instance it received.

// Arrange
var obj = new MyClass { MyProperty = "Hello world !" };
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomethingDifferent(obj);

// Assert
stub.AssertWasCalled(mock => mock.MyOtherMethod(
	With<MyOtherClass>.Like(other => other.MyProperty == obj.MyProperty)));

Scenario 6: we want to verity that our SUT calls MyMethodOnMultiple() on the injected component and passes an IEnumerable of the references to the object that it received.

// Arrange
var array = new[]
{
	new MyClass { MyProperty = "Value 1" },
	new MyClass { MyProperty = "Value 2" }
};
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomehtingOnMultiple(array);

// Assert
stub.AssertWasCalled(mock => mock.MyMethodOnMultiple(
	With.Enumerable(array).Like<MyClass>((source, item) => source == item)));

Scenario 7: we want to verity that our SUT calls MyOtherMethodOnMultiple() on the injected component and passes an IEnumerable of different type that match (using a predicate) the IEnumerable it received.

// Arrange
var array = new[]
{
	new MyClass { MyProperty = "Value 1" },
	new MyClass { MyProperty = "Value 2" }
};
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomehtingDifferentOnMultiple(array);

// Assert
stub.AssertWasCalled(mock => mock.MyOtherMethodOnMultiple(
	With.Enumerable(array)
		.Like<MyOtherClass>((source, item) => source.MyProperty == item.MyProperty)));

Scenario 8: we want to verify that our SUT does not call MyOtherMethodOnMultiple() on the injected component.

// Arrange
var array = new MyClass[0];
var stub = new StubIMyComponent().AsObservable();
var sut = MakeSut(stub);

// Act
sut.DoSomehtingDifferentOnMultiple(array);

// Assert
stub.AssertWasNotCalled(mock => mock.MyOtherMethodOnMultiple(
	With.Enumerable(array)
		.Like<MyOtherClass>((source, item) => source.MyProperty == item.MyProperty)));

Scenario 9: we want to verify that our SUT does call a static property on a .NET component that was shimed.

using (var context = ObservableShimsContext.Create())
{
	// Arrange
	var now = new DateTime(2013, 1, 1);
	ShimDateTime.NowGet = () => now;
	var sut = MakeSut();
	
	// Act
	sut.ReturnDateTimeNow();
	
	// Assert
	context.AssertWasCalled(() => DateTime.Now);
}

Scenario 10: we want to verify that our SUT does call a static method on a .NET component that was shimed.

using (var context = ObservableShimsContext.Create())
{
	// Arrange
	ShimGuid.NewGuid = () => new Guid("97f31de6-5a03-454a-976f-76e2a54a89d4");
	var sut = MakeSut();
	
	// Act
	sut.ReturnNewGuid();
	
	// Assert
	context.AssertWasCalled(() => Guid.NewGuid());
}

Scenario 11: we want to verify that our SUT does call a method on an instantiated .NET component that was shimed. Note that with the current implementation of the shims' detour, the parameter cannot be verified.

using (var context = ObservableShimsContext.Create())
{
    // Arrange
    const string url = "http://www.dev-one.com";
    var sut = MakeSut();
    ShimHttpClient.AllInstances.GetStringAsyncString = (c, u) => Task.FromResult("ok");

    // Act
    await sut.GoOnTheWeb(url);

    // Assert (ATM, parameter cannot be verified)
    context.AssertWasCalled((HttpClient client) => client.GetStringAsync(url));
}

Release notes

  • Version 0.8:
    • added SourceLink support
    • minor code cleanup based on static analysis
    • adding support for shims with void returns
  • Version 0.7:
    • added support for interface types
    • removed assembly signing
    • updated copyright and maintainer/owner information
  • Version 0.6: added an ObservableShimsContext to allow verifications on shims
  • Version 0.5: added an AssertWasNotCalled assertion
  • Version 0.4: improved With to allow predicate of two different types
  • Version 0.3: added more flexible ways to verify a mock
  • Version 0.2: first usable version. Watch out, this is still apha code !

About the authors

This package is maintained by John M. Wright [ Twitter: @Wright2Tweet ]

Fakes.Contrib was originally written and maintained by Fabian Vilers and Dev One. [ Twitter: @fvilers ]

Release Notes

Version 0.8:
 * added SourceLink support
 * minor code cleanup based on static analysis
 * adding support for shims with void returns
Version 0.7:
 * added support for interface types
 * removed assembly signing
 * updated copyright and maintainer/owner information
Version 0.6: added an "ObservableShimsContext" to allow verifications on shims
Version 0.5: added an "AssertWasNotCalled" assertion
Version 0.4: improved "With" to allow predicate of two different types
Version 0.3: added more flexible ways to verify a mock
Version 0.2: first usable version. Watch out, this is still apha code !

Version History

Version Downloads Last updated
0.8.18200.2 137 7/19/2018
0.7.18065.8 249 3/6/2018
0.6.5389.13898 3,857 10/3/2014
0.6.5099.26816 404 12/17/2013
0.6.5095.20427 272 12/13/2013
0.5.5073.15041 295 11/21/2013
0.5.5059.17884 296 11/7/2013
0.4.5045.16141 318 10/24/2013
0.4.5045.15763 274 10/24/2013
0.4.5045.14338 273 10/24/2013
0.3.5044.26836 311 10/23/2013
0.2.5044.18289 273 10/23/2013