Last active
December 14, 2015 03:09
-
-
Save k0stya/5019162 to your computer and use it in GitHub Desktop.
Replace a component that the system under test (SUT) depends on with a much lighter-weight implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TestDoubles.Examples.Fake | |
{ | |
[TestClass] | |
public class Invoice_Test | |
{ | |
[TestMethod] | |
public void AddItemQuantity_should_add_item_quantity() | |
{ | |
// Arrange | |
var fakeRepository = new FakeRepository(); | |
var invoice = new Invoice(fakeRepository); | |
var product = new Product { Id = 1 }; | |
// Act | |
invoice.AddItemQuantity(product, 10); | |
// Assert | |
Assert.AreEqual(product, fakeRepository.GetProduct(1)); | |
} | |
} | |
public class Invoice | |
{ | |
private readonly IProductRepository _repository; | |
public Invoice(IProductRepository repository) | |
{ | |
_repository = repository; | |
} | |
/* Methods that use repository*/ | |
public void AddItemQuantity(Product product, int i) | |
{ | |
_repository.AddProduct(product, i); | |
} | |
} | |
public class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
public interface IProductRepository | |
{ | |
Product AddProduct(Product product, int i); | |
Product GetProduct(int productId); | |
} | |
public class FakeRepository : IProductRepository | |
{ | |
private List<Product> _list = new List<Product>(); | |
public Product AddProduct(Product product, int i) | |
{ | |
_list.Add(product); | |
/* Some additional logic */ | |
return product; | |
} | |
public Product GetProduct(int productId) | |
{ | |
return _list.SingleOrDefault(p => p.Id == productId); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment