Excerpt from the Book Pro ASP. NET MVC3 Framework:
Moq Technology Used in Unit Test [Add a reference to Moq. dll in the Unit Test project]
Interface Definition:
Public interface IProductRepository
{
IEnumerable <Product> GetProducts ();
}
Interface implementation class:
Public class FakeRepository: IProductRepository
{
// 275 M, followed by m or M, indicating a decimal type, 128 bits
Private Product [] products = new Product [] {
New Product () {Name = "Kayak", Price = 275 M },
New Product () {Name = "Lifejacket", Price = 48.95 M },
New Product () {Name = "Soccer ball", Price = 19.50 M },
New Product () {Name = "Stadium", Price = 79500 M}
};
Public IEnumerable <Product> GetProducts ()
{
Return products;
}
Public decimal GetTotalValue ()
{
Return products. Sum (e => e. Price );
}
}
Another interface:
Public interface IPriceReducer
{
Void performanceprices (decimal priceReduction );
}
Implementation class of this interface:
Public class MyPriceReducer: IPriceReducer
{
Private IProductRepository repository;
// Depends on the IProductRepository Interface
Public MyPriceReducer (IProductRepository repo)
{
This. repository = repo;
}
Public void performanceprices (decimal priceReduction)
{
Foreach (var item in repository. GetProducts ())
{
Item. Price = Math. Max (item. Price-priceReduction, 1 );
Repository. UpdateProduct (item );
}
}
}
Unit test cases without Moq technology: [requires the FakeRepository class in the project]
[TestMethod]
Public void correcttotalreduamoamount ()
{
//
FakeRepository repo = new FakeRepository ();
Decimal reduamoamount = 10;
Decimal initialTotal = repo. GetTotalValue ();
MyPriceReducer target = new MyPriceReducer (repo );
Target. performanceprices (reduamoamount );
Assert. AreEqual (repo. GetTotalValue (),
(InitialTotal-(repo. GetProducts (). Count () * reduamoamount ))
);
}
Unit test cases using Moq technology:
[TestMethod]
Public void CorrectTotalReductionAmount2 ()
{
Product [] products = new Product [] {
New Product () {Name = "Kayak", Price = 275 M },
New Product () {Name = "Lifejacket", Price = 48.95 M },
New Product () {Name = "Soccer ball", Price = 19.50 M },
New Product () {Name = "Stadium", Price = 79500 M}
};
// At this time, the Implementation class of the IProductRepository interface is not required, that is, the project does not need to write an implementation class of the IProductRepository interface. Moq can simulate such an implementation class,
Mock <IProductRepository> mock = new Mock <IProductRepository> ();
// When the GetProducts () method in IProductRepository is called, products is returned.
Mock. Setup (m => m. GetProducts (). Returns (products );
Decimal reduamoamount = 10;
Decimal initialTotal = products. Sum (p => p. Price );
// Pass the Moq simulated object to MyPriceReducer
MyPriceReducer target = new MyPriceReducer (mock. Object );
Target. performanceprices (reduamoamount );
Assert. AreEqual (products. Sum (p => p. Price ),
(InitialTotal-(products. Count () * reductionAmount )));
}
// The above data source products is declared in a test method. To make the code concise, you can declare products in this test class. [This way, you can reference other test methods of the data source ], for example:
[TestClass ()]
Public class MyPriceReducerTest
{
Private IEnumerable <Product> products;
[TestInitialize]
Public void PreInitialize ()
{
Products = new Product [] {
New Product () {Name = "Kayak", Price = 275 M },
New Product () {Name = "Lifejacket", Price = 48.95 M },
New Product () {Name = "Soccer ball", Price = 19.50 M },
New Product () {Name = "Stadium", Price = 79500 M}
};
}
}
Attribute [TestInitialize]: The PreInitialize () method is called before each test case is run.