Created
December 1, 2011 04:23
-
-
Save MattHoneycutt/1413596 to your computer and use it in GitHub Desktop.
SpecsFor Examples
This file contains 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
protected override void Given() | |
{ | |
//Access Moq objects easily | |
GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(true) | |
.Verifiable(); | |
} | |
protected override void When() | |
{ | |
//The class under test is created automatically, no need to manually | |
//wire up dependencies with mock objects. | |
_result = SUT.Process(new Order { PartNumber = "TestPart", Quantity = 10 }); | |
} | |
[Test] | |
public void then_the_order_is_accepted() | |
{ | |
//Use Should to write fluent asserts. | |
_result.WasAccepted.ShouldBeTrue(); | |
} | |
... | |
[Test] | |
public void then_it_returns_a_successful_result() | |
{ | |
//Utilize ExpectedObjects via new SpecsFor extension methods. | |
_result.ShouldLookLike(new InvoiceSubmissionResult {Accepted = true, AcceptedDate = DateTime.Today}); | |
} | |
[Test] | |
public void then_it_publishes_an_event() | |
{ | |
//The Looks.Like method simplifies the creation of complex mock expectations. | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(Looks.Like(new InvoiceSubmittedEvent | |
{ | |
BillingName = _invoice.BillingName, | |
BillingAddress = _invoice.BillingAddress, | |
BillingCity = _invoice.BillingCity, | |
BillingZip = _invoice.BillingZip, | |
Amount = _invoice.Amount, | |
Description = _invoice.Description | |
}))); | |
} |
This file contains 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
//BDD-Style Tests | |
public class OrderProcessorSpecs | |
{ | |
public class given_the_item_is_available_when_processing_an_order : SpecsFor<OrderProcessor> | |
{ | |
private OrderResult _result; | |
protected override void Given() | |
{ | |
GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(true) | |
.Verifiable(); | |
} | |
protected override void When() | |
{ | |
_result = SUT.Process(new Order { PartNumber = "TestPart", Quantity = 10 }); | |
} | |
[Test] | |
public void then_the_order_is_accepted() | |
{ | |
_result.WasAccepted.ShouldBeTrue(); | |
} | |
[Test] | |
public void then_it_checks_the_inventory() | |
{ | |
GetMockFor<IInventory>().Verify(); | |
} | |
[Test] | |
public void then_it_raises_an_order_submitted_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.Is<OrderSubmitted>(o => o.OrderNumber == _result.OrderNumber))); | |
} | |
} | |
} | |
... | |
//Old-school test methods | |
public class OrderProcessorSpecs : SpecsFor<OrderProcessor> | |
{ | |
[Test] | |
public void Order_submitted_successfully_Tests() | |
{ | |
GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(true) | |
.Verifiable(); | |
var result = SUT.Process(new Order {PartNumber = "TestPart", Quantity = 10}); | |
result.WasAccepted.ShouldBeTrue(); | |
GetMockFor<IInventory>().Verify(); | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.Is<OrderSubmitted>(o => o.OrderNumber == result.OrderNumber))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment