Your
Code
Test
[TestMethod]
public void Method_Scenario_Result()
{
// Set dependencies, build SUT, set
// expectations
// Exercise the SU...
[TestMethod]
public void Method_Scenario_Result()
{
// Set dependencies, build SUT, set
// expectations
// Exercise the SU...
[Test]
public void CreateNumString_TwoSimpleNumbers_ReturnsStringWithCommaBetween()
{
StringCalc sc = new StringCalc();
st...
SUT Dependancy
Dependancy
Dependency
public class Sample{
private DatabaseThingie myDatabase;
public Sample() {
myDatabase = new DatabaseThingie();
}
public vo...
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Unit Testing and Mocking using MOQ
Upcoming SlideShare
Loading in …5
×

Unit Testing and Mocking using MOQ

1,551 views

Published on

A brief description of unit testing, with a more thorough walkthrough of how to mock using MOQ. The demo code can be found at http://1drv.ms/1kQJWhb

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,551
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
41
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • publicvoidProductService_Create_CallsRepositorySave() {varmockProductRepository=newMock<IProductRepository>();mockProductRepository.Setup(r=>r.Save(It.IsAny<Product>()));varservice=newProductService(mockProductRepository.Object);service.Create(newProductViewModel());mockProductRepository.VerifyAll(); }
  • [Test] [ExpectedException(typeof (InvalidProductIdException))]publicvoidProductService_Create_ExceptionIfIdNotCreated() {ProductViewModelproductViewModel=newProductViewModel() { Description="Nice product", Name="ProductA" };varmockProductRepository=newMock<IProductRepository>();mockProductRepository.Setup(r=>r.Save(It.IsAny<Product>()));varmockProductIdBuilder=newMock<IProductIdBuilder>();varservice=newProductService(mockProductRepository.Object, mockProductIdBuilder.Object);service.Create(productViewModel); }
  • [Test]publicvoidProductService_CreateMany_VerifyCallsWereMade() {List<ProductViewModel>productViewModels=newList<ProductViewModel>() {newProductViewModel(){Name="ProductA", Description="Great product"},newProductViewModel(){Name="ProductB", Description="Bad product"},newProductViewModel(){Name="ProductC", Description="Cheap product"},newProductViewModel(){Name="ProductD", Description="Expensive product"} };varmockProductRepository=newMock<IProductRepository>();varmockIdBuilder=newMock<IProductIdBuilder>();mockIdBuilder.Setup(b=>b.BuildProductIdentifier()).Returns(newProductIdentifier());varservice=newProductService(mockProductRepository.Object, mockIdBuilder.Object);service.CreateMany(productViewModels);mockProductRepository.Verify(p=>p.Save(It.IsAny<Product>()), Times.Exactly(productViewModels.Count)); }
  • publicvoidProductService_CreateMany_VerifyCallsWereMadeWithCallbacks() {List<ProductViewModel>productViewModels=newList<ProductViewModel>() {newProductViewModel(){Name="ProductA", Description="Great product"},newProductViewModel(){Name="ProductB", Description="Bad product"},newProductViewModel(){Name="ProductC", Description="Cheap product"},newProductViewModel(){Name="ProductD", Description="Expensive product"} };List<int>productIds=newList<int>();varmockProductRepository=newMock<IProductRepository>();mockProductRepository.Setup(r=>r.Save(It.IsAny<Product>())).Callback<Product>(p=>productIds.Add(p.Identifier.RawValue));varmockIdBuilder=newMock<IProductIdBuilder>();intproductId=1;mockIdBuilder.Setup(b=>b.BuildProductIdentifier()).Returns(() =>newProductIdentifier(){RawValue=productId}).Callback(() =>newProductIdentifier() { RawValue=productId++ });varservice=newProductService(mockProductRepository.Object, mockIdBuilder.Object);service.CreateMany(productViewModels);mockProductRepository.Verify(p=>p.Save(It.IsAny<Product>()), Times.Exactly(productViewModels.Count));vargroupings=fromxinproductIdsgroupxbyxintogletcount=g.Count()orderbycountdescendingselectnew { Value=g.Key, Count=count };foreach (varxingroupings)Assert.IsTrue(x.Count==1); } }
  • [Test]publicvoidProductService_CallCreate_TryThreeTimesOnFailedSave() {varmockProductRepository=newMock<IProductRepository>();mockProductRepository.Setup(r=>r.Save(It.IsAny<Product>())).Throws(newDbConnectionException());varmockIdBuilder=newMock<IProductIdBuilder>();mockIdBuilder.Setup(b=>b.BuildProductIdentifier(It.IsAny<string>())).Returns(newProductIdentifier());varservice=newProductService(mockProductRepository.Object, mockIdBuilder.Object);service.Create(newProductViewModel());mockProductRepository.Verify(r=>r.Save(It.IsAny<Product>()), Times.Exactly(3)); }
  • Unit Testing and Mocking using MOQ

    1. 1. Your Code Test
    2. 2. [TestMethod] public void Method_Scenario_Result() { // Set dependencies, build SUT, set // expectations // Exercise the SUT // Check behaviour/state of SUT // Check that expectations have been met } Assert Act Arrange
    3. 3. [TestMethod] public void Method_Scenario_Result() { // Set dependencies, build SUT, set // expectations // Exercise the SUT // Check behaviour/state of SUT // Check that expectations have been met } Assert Act Arrange
    4. 4. [Test] public void CreateNumString_TwoSimpleNumbers_ReturnsStringWithCommaBetween() { StringCalc sc = new StringCalc(); string result = sc.CreateNumString(1, 2); Assert.AreEqual(String.Format("{0},{1}", x, y), result); }
    5. 5. SUT Dependancy Dependancy Dependency
    6. 6. public class Sample{ private DatabaseThingie myDatabase; public Sample() { myDatabase = new DatabaseThingie(); } public void DoStuff() { myDatabase.GetData(); } }

    ×