Mvc3 (2)

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.