Your SlideShare is downloading. ×
  • Like
Unit Testing and Mocking using MOQ
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Unit Testing and Mocking using MOQ

  • 412 views
Published

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

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
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
412
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
10
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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)); }

Transcript

  • 1. Your Code Test
  • 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. [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. [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. SUT Dependancy Dependancy Dependency
  • 6. public class Sample{ private DatabaseThingie myDatabase; public Sample() { myDatabase = new DatabaseThingie(); } public void DoStuff() { myDatabase.GetData(); } }