以下の内容はhttps://cheatography.com///gregfinzer/cheat-sheets/rhinomocks/より取得しました。


Cheatography

RhinoMocks Cheat Sheet by

Example for RhinoMocks Mocking Framework

Example Code

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

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

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

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

}

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

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

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

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

Comments

No comments yet. Add yours below!

Add a Comment

Related Cheat Sheets

Regular Expressions Cheat Sheet
  Clean Code Cheat Sheet by Urs Enzler

More Cheat Sheets by GregFinzer

Loving Your Neighbor Cheat Sheet
AWS Core Service Options Cheat Sheet
Unity Container Cheat Sheet



以上の内容はhttps://cheatography.com///gregfinzer/cheat-sheets/rhinomocks/より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14