RhinoMocks Cheat Sheet

A cheat sheet in English by GregFinzer

Example for RhinoMocks Mocking Framework

6,901 views 1,748 downloads 12 Dec 2017 History
3.7 avg · 0 ratings

Example Code

public interface ISalesTaxService
{
	&nbsp;&nbsp;&nbsp;&nbsp;Dictionary<string, decimal> GetSalesTaxByPostalCode(string country);
}

public class SalesTaxLogic 
{
	&nbsp;&nbsp;&nbsp;&nbsp;private readonly Dictionary<string, decimal> _salesTaxByPostalCode;

	&nbsp;&nbsp;&nbsp;&nbsp;public SalesTaxLogic(ISalesTaxService salesTaxService, string country)
	&nbsp;&nbsp;&nbsp;&nbsp;{
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_salesTaxByPostalCode = salesTaxService.GetSalesTaxByPostalCode(country);
	&nbsp;&nbsp;&nbsp;&nbsp;}

	&nbsp;&nbsp;&nbsp;&nbsp;public decimal CalcSalesTax(string postalCode, decimal orderTotal)
	&nbsp;&nbsp;&nbsp;&nbsp;{
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Math.Round(_salesTaxByPostalCode[postalCode] * orderTotal, 2, MidpointRounding.AwayFromZero);
	&nbsp;&nbsp;&nbsp;&nbsp;}

}

[TestClass]
public class InvoiceLogicTests
{
	&nbsp;&nbsp;&nbsp;&nbsp;[TestMethod]
	&nbsp;&nbsp;&nbsp;&nbsp;public void When_Calculating_Sales_Tax_It_Should_Not_Be_Zero()
	&nbsp;&nbsp;&nbsp;&nbsp;{
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Arrange
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ISalesTaxService salesTaxServiceMock = MockRepository.GenerateMock<ISalesTaxService>();
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dictionary<string, decimal> salesTaxRates = new Dictionary<string, decimal>();
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salesTaxRates.Add("43229", .0675M);
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salesTaxServiceMock.Stub(o => o.GetSalesTaxByPostalCode(null)).IgnoreArguments().Return(salesTaxRates).Repeat.Any();

		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SalesTaxLogic salesTaxLogic = new SalesTaxLogic(salesTaxServiceMock, "USA");

		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Act
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decimal orderSalesTax = salesTaxLogic.CalcSalesTax("43229", 19.95M);

		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Assert
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(orderSalesTax);
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.AreNotEqual(0, orderSalesTax);
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salesTaxServiceMock.AssertWasCalled(o => o.GetSalesTaxByPostalCode("USA"));
	&nbsp;&nbsp;&nbsp;&nbsp;}
}

Badges

Nice Cheat Sheet Good Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.