Created
November 18, 2011 04:30
-
-
Save MattHoneycutt/1375611 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
public class InvoiceProcessorSpecs | |
{ | |
public class when_processing_an_invoice_with_helpers : SpecsFor<InvoiceProcessor> | |
{ | |
... | |
[Test] | |
public void then_it_publishes_an_event() | |
{ | |
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
public class InvoiceProcessorSpecs | |
{ | |
public class when_processing_an_invoice_without_helpers : SpecsFor<InvoiceProcessor> | |
{ | |
... | |
[Test] | |
public void then_it_publishes_an_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.Is<InvoiceSubmittedEvent>(e => | |
e.BillingName == _invoice.BillingName && | |
e.BillingAddress == _invoice.BillingAddress && | |
e.BillingCity == _invoice.BillingCity && | |
e.BillingZip == _invoice.BillingZip && | |
e.Amount == _invoice.Amount && | |
e.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
public class InvoiceProcessorSpecs | |
{ | |
public class when_processing_an_invoice_with_helpers : SpecsFor<InvoiceProcessor> | |
{ | |
... | |
[Test] | |
public void then_it_returns_a_successful_result() | |
{ | |
_result.ShouldLookLike(new InvoiceSubmissionResult {Accepted = true, AcceptedDate = DateTime.Today}); | |
} | |
} | |
} |
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
public class InvoiceProcessorSpecs | |
{ | |
public class when_processing_an_invoice_with_helpers : SpecsFor<InvoiceProcessor> | |
{ | |
private InvoiceSubmissionResult _result; | |
private Invoice _invoice; | |
protected override void When() | |
{ | |
_invoice = new Invoice | |
{ | |
BillingName = "John D. Doe", | |
BillingAddress = "123 Happy Street", | |
BillingCity = "Imagination Land", | |
BillingZip = "12345", | |
Amount = 5.35m, | |
Description = "Work" | |
}; | |
_result = SUT.SubmitInvoice(_invoice); | |
} | |
[Test] | |
public void then_it_returns_a_successful_result() | |
{ | |
_result.ShouldLookLike(new InvoiceSubmissionResult {Accepted = true, AcceptedDate = DateTime.Today}); | |
} | |
[Test] | |
public void then_it_publishes_an_event() | |
{ | |
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
public class InvoiceProcessorSpecs | |
{ | |
public class when_processing_an_invoice_without_helpers : SpecsFor<InvoiceProcessor> | |
{ | |
private InvoiceSubmissionResult _result; | |
private Invoice _invoice; | |
protected override void When() | |
{ | |
_invoice = new Invoice | |
{ | |
BillingName = "John D. Doe", | |
BillingAddress = "123 Happy Street", | |
BillingCity = "Imagination Land", | |
BillingZip = "12345", | |
Amount = 5.35m, | |
Description = "Work" | |
}; | |
_result = SUT.SubmitInvoice(_invoice); | |
} | |
[Test] | |
public void then_it_returns_a_successful_result() | |
{ | |
_result.Accepted.ShouldBeTrue(); | |
_result.AcceptedDate.ShouldEqual(DateTime.Today); | |
} | |
[Test] | |
public void then_it_publishes_an_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.Is<InvoiceSubmittedEvent>(e => | |
e.BillingName == _invoice.BillingName && | |
e.BillingAddress == _invoice.BillingAddress && | |
e.BillingCity == _invoice.BillingCity && | |
e.BillingZip == _invoice.BillingZip && | |
e.Amount == _invoice.Amount && | |
e.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
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))); | |
} | |
} | |
public class given_the_item_is_not_available_when_processing_an_order : SpecsFor<OrderProcessor> | |
{ | |
private OrderResult _result; | |
protected override void Given() | |
{ | |
GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(false) | |
.Verifiable(); | |
} | |
protected override void When() | |
{ | |
_result = SUT.Process(new Order { PartNumber = "TestPart", Quantity = 10 }); | |
} | |
[Test] | |
public void then_the_order_is_rejected() | |
{ | |
_result.WasAccepted.ShouldBeFalse(); | |
} | |
[Test] | |
public void then_it_checks_the_inventory() | |
{ | |
GetMockFor<IInventory>().Verify(); | |
} | |
[Test] | |
public void then_it_does_not_raise_an_order_submitted_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never()); | |
} | |
} | |
} |
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
public class OrderProcessorSpecs | |
{ | |
public class when_processing_an_order : given.the_item_is_available | |
{ | |
private OrderResult _result; | |
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))); | |
} | |
} | |
public class when_processing_an_order_with_a_negative_quantity : given.the_item_is_available | |
{ | |
private OrderResult _result; | |
protected override void When() | |
{ | |
_result = SUT.Process(new Order{ PartNumber = "TestPart", Quantity = -1}); | |
} | |
[Test] | |
public void then_the_order_is_rejected() | |
{ | |
_result.WasAccepted.ShouldBeFalse(); | |
} | |
[Test] | |
public void then_it_does_not_check_the_inventory() | |
{ | |
GetMockFor<IInventory>() | |
.Verify(i => i.IsQuantityAvailable("TestPart", -1), Times.Never()); | |
} | |
[Test] | |
public void then_it_does_not_raise_an_order_submitted_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never()); | |
} | |
} | |
public class given | |
{ | |
public class the_item_is_available : SpecsFor<OrderProcessor> | |
{ | |
protected override void Given() | |
{ | |
GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(true) | |
.Verifiable(); | |
} | |
} | |
} | |
} |
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
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))); | |
} | |
[Test] | |
public void Order_is_rejected_Tests() | |
{ | |
GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(false) | |
.Verifiable(); | |
var result = SUT.Process(new Order {PartNumber = "TestPart", Quantity = 10}); | |
result.WasAccepted.ShouldBeFalse(); | |
GetMockFor<IInventory>().Verify(); | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never()); | |
} | |
} |
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
public class ValidatingOrderProcessorSpecs | |
{ | |
[Given(typeof(the_item_is_available), typeof(the_order_is_valid))] | |
public class when_processing_an_order : SpecsFor<ValidatingOrderProcessor> | |
{ | |
private OrderResult _result; | |
public when_processing_an_order(Type[] contexts) : base(contexts){} | |
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))); | |
} | |
} | |
[Given(typeof(the_order_is_not_valid))] | |
[Given(typeof(the_item_is_not_available))] | |
public class when_processing_an_an_invalid_order : SpecsFor<ValidatingOrderProcessor> | |
{ | |
private OrderResult _result; | |
public when_processing_an_an_invalid_order(Type[] context) : base(context) {} | |
protected override void When() | |
{ | |
_result = SUT.Process(new Order{ PartNumber = "TestPart", Quantity = -1}); | |
} | |
[Test] | |
public void then_the_order_is_rejected() | |
{ | |
_result.WasAccepted.ShouldBeFalse(); | |
} | |
[Test] | |
public void then_it_does_not_raise_an_order_submitted_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never()); | |
} | |
} | |
public class the_item_is_available : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(true) | |
.Verifiable(); | |
} | |
} | |
public class the_order_is_valid : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IValidator<Order>>() | |
.Setup(v => v.Validate(It.IsAny<Order>())) | |
.Returns(true); | |
} | |
} | |
public class the_order_is_not_valid : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IValidator<Order>>() | |
.Setup(v => v.Validate(It.IsAny<Order>())) | |
.Returns(false); | |
} | |
} | |
public class the_item_is_not_available : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable(It.IsAny<string>(), It.IsAny<int>())) | |
.Returns(false); | |
} | |
} | |
} |
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
public class ValidatingOrderProcessorSpecs | |
{ | |
public class when_processing_an_order : SpecsFor<ValidatingOrderProcessor> | |
{ | |
private OrderResult _result; | |
protected override void Given() | |
{ | |
Given<the_item_is_available>(); | |
Given<the_item_is_valid>(); | |
base.Given(); | |
} | |
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))); | |
} | |
} | |
public class when_processing_an_order_with_a_negative_quantity : SpecsFor<ValidatingOrderProcessor> | |
{ | |
private OrderResult _result; | |
protected override void Given() | |
{ | |
Given<the_item_is_available>(); | |
Given<the_item_is_not_valid>(); | |
base.Given(); | |
} | |
protected override void When() | |
{ | |
_result = SUT.Process(new Order{ PartNumber = "TestPart", Quantity = -1}); | |
} | |
[Test] | |
public void then_the_order_is_rejected() | |
{ | |
_result.WasAccepted.ShouldBeFalse(); | |
} | |
[Test] | |
public void then_it_does_not_check_the_inventory() | |
{ | |
GetMockFor<IInventory>() | |
.Verify(i => i.IsQuantityAvailable("TestPart", -1), Times.Never()); | |
} | |
[Test] | |
public void then_it_does_not_raise_an_order_submitted_event() | |
{ | |
GetMockFor<IPublisher>() | |
.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never()); | |
} | |
} | |
public class the_item_is_available : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IInventory>() | |
.Setup(i => i.IsQuantityAvailable("TestPart", 10)) | |
.Returns(true) | |
.Verifiable(); | |
} | |
} | |
public class the_item_is_valid : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IValidator<Order>>() | |
.Setup(v => v.Validate(It.IsAny<Order>())) | |
.Returns(true); | |
} | |
} | |
public class the_item_is_not_valid : IContext<ValidatingOrderProcessor> | |
{ | |
public void Initialize(ITestState<ValidatingOrderProcessor> state) | |
{ | |
state.GetMockFor<IValidator<Order>>() | |
.Setup(v => v.Validate(It.IsAny<Order>())) | |
.Returns(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment