9. TFD Details
A Jig is a form upon which
something else is built
The jig is the specification for
the thing being built
Remove the jig when ready to
Launch
For software, Jig is built just a moment before it is being used
10. Create a failing test Red
Make the test pass Green
RED Refactor Improve the internal
implementation without
changing the external
contract or behavior
Refactor Green
17. Setting Up A Test Project
Tests live in a separate class library project
18. A First Test
Using Nunit.Framework
Namespace Domain.Tests
{
[TestFixture]
public class FirstTestFixture
{
[Test]
public void AFirstTest()
{
Assert.IsTrue(true,”true is true!”);
}
}
}
19. TFD - Assertions
Use one logical assertion per test
[Test]
public void the_order_is_canceled()
{
var customer = CreateCustomer();
Assert.IsNotNull(customer);
customer.PlaceOrder();
Assert.IsTrue(customer.HasOrder);
customer.CancelOrder();
Assert.IsFalse(customer.HasOrder);
}
23. Faking out the SUT
Test Method
1.Create test specific objects
2.Create the SUT (using Interface)
3. Invoke operation on the SUT
4.Check results of SUT invocation On the SUT
25. Dummy
var person = new Person();
person.First = “Homer”;
person.Last = “Simpson”;
Assert.IsNotNull(person.FullName);
var order = new Order();
order.AddLineItem(12, 1);
order.AddLineItem.Add(21, 3);
Assert.AreEqual(2, order.NumLineItems);
26. Stub
public class StubRepo : IOwnerRepository {
public IOwner FindById(int id){}
public IOwner Save(IOwner owner) {
return new Owner();
}
public void Delete(IOwner owner){}
}
29. e.g.
using System;
using System.Collections.Generic;
namespace MoqSamples.Models
{
public interface IProductRepository
{
List<IProduct> Select();
IProduct Get(int id);
}
public interface IProduct
{
int Id {get; set;}
string Name { get; set; }
}
}
// Mock a product
var newProduct = new Mock<IProduct>();
newProduct.ExpectGet(p => p.Id).Returns(1);
newProduct.ExpectGet(p => p.Name).Returns("Bushmills");
Assert.AreEqual("Bushmills", newProduct.Object.Name);
30. e.g.
// Mock product repository
var productRepository = new Mock<IProductRepository>();
productRepository.Expect(p => p.Get(1)).Returns(newProduct.Object)
// Act
var productReturned = productRepository.Object.Get(1);
// Assert
Assert.AreEqual("Bushmills", productReturned.Name);
// Mock product repository
var productRepository = new Mock<IProductRepository>();
productRepository
.Expect(p => p.Get(It.IsAny<int>()))
.Returns(newProduct.Object);
31. Summery…
Stick with red
-
Write tests in a green
separate -
project refactor
Keep
Treat test code practicing and
with respect learning
32. Reference
Test Driven Development by Example , Kent Beck, 2002
http://code.google.com/p/moq/wiki/QuickStart
http://stephenwalther.com/blog/archive/2008/06/12/tdd-
introduction-to-moq.aspx
Source: Pluralsight,Google,WIKI