Fakes.Contrib 0.8.18200.2

dotnet add package Fakes.Contrib --version 0.8.18200.2                
NuGet\Install-Package Fakes.Contrib -Version 0.8.18200.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fakes.Contrib" Version="0.8.18200.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fakes.Contrib --version 0.8.18200.2                
#r "nuget: Fakes.Contrib, 0.8.18200.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Fakes.Contrib as a Cake Addin
#addin nuget:?package=Fakes.Contrib&version=0.8.18200.2

// Install Fakes.Contrib as a Cake Tool
#tool nuget:?package=Fakes.Contrib&version=0.8.18200.2                

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 ]

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.8.18200.2 1,773 7/19/2018
0.7.18065.8 1,157 3/6/2018
0.6.5389.13898 5,009 10/3/2014
0.6.5099.26816 1,306 12/17/2013
0.6.5095.20427 1,192 12/13/2013
0.5.5073.15041 1,310 11/21/2013
0.5.5059.17884 1,214 11/7/2013
0.4.5045.16141 1,372 10/24/2013
0.4.5045.15763 1,239 10/24/2013
0.4.5045.14338 1,198 10/24/2013
0.3.5044.26836 1,415 10/23/2013
0.2.5044.18289 1,217 10/23/2013

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 !