Created
June 12, 2010 00:18
-
-
Save cprieto/435228 to your computer and use it in GitHub Desktop.
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
using Machine.Specifications; | |
using Moq; | |
using That = Moq.It; | |
using It = Machine.Specifications.It; | |
namespace Webdevelopment.Hif.Impression | |
{ | |
public abstract class TrackingSpecificationContext | |
{ | |
protected static ITrackingService sut; | |
protected static Mock<IClickImpressionService> clickSvc; | |
protected static Mock<IBalanceService> balanceSvc; | |
protected static Mock<IClickValidatorService> validatorSvc; | |
protected static ClickImpression click; | |
protected static int transactionId; | |
protected const int AccountId = 1001; | |
Establish context = () => | |
{ | |
click = new ClickImpression {AdvertiserAccountId = AccountId}; | |
clickSvc = new Mock<IClickImpressionService>(); | |
validatorSvc = new Mock<IClickValidatorService>(); | |
balanceSvc = new Mock<IBalanceService>(); | |
sut = new TrackingService(clickSvc.Object, balanceSvc.Object); | |
clickSvc | |
.Setup(m => m.SaveTransaction(click)) | |
.Returns(1000); | |
validatorSvc | |
.Setup(m => m.Validate(click)) | |
.Returns(true); | |
}; | |
} | |
[Subject(typeof(TrackingService), "with valid clicks and not filtered")] | |
public class When_advertiser_has_enough_funds : TrackingSpecificationContext | |
{ | |
Because of = () => | |
transactionId = sut.TrackClick(click); | |
It should_save_click = () => | |
clickSvc.Verify(m => m.SaveTransaction(click)); | |
It should_check_balance = () => | |
balanceSvc.Verify(m => m.AccountHasBalance(AccountId)); | |
It should_return_transaction_id = () => | |
transactionId.ShouldBeGreaterThan(0); | |
} | |
[Subject(typeof(TrackingService), "with valid clicks and not filtered")] | |
public class When_advertiser_does_not_have_enough_funds : TrackingSpecificationContext | |
{ | |
Because of = () => | |
{ | |
balanceSvc | |
.Setup(m => m.AccountHasBalance(AccountId)) | |
.Returns(false); | |
click.Filtered = false; | |
transactionId = sut.TrackClick(click); | |
}; | |
It should_save_click = () => | |
clickSvc.Verify(m => m.SaveTransaction(click)); | |
It should_filter_click = () => | |
click.Filtered.ShouldBeTrue(); | |
It should_check_balance = () => | |
balanceSvc.Verify(m => m.AccountHasBalance(AccountId)); | |
It should_return_transaction_id = () => | |
transactionId.ShouldBeGreaterThan(0); | |
} | |
[Subject(typeof(TrackingService), "with valid but filtered clicks")] | |
public class When_click_is_filtered : TrackingSpecificationContext | |
{ | |
Because of = () => | |
{ | |
click.Filtered = true; | |
transactionId = sut.TrackClick(click); | |
}; | |
It should_save_click = () => | |
clickSvc.Verify(m => m.SaveTransaction(click)); | |
It should_not_check_balance = () => | |
balanceSvc.Verify(m => m.AccountHasBalance(AccountId), Times.Never()); | |
It should_return_transaction_id = () => | |
transactionId.ShouldBeGreaterThan(0); | |
} | |
[Subject(typeof(TrackingService), "with invalid clicks")] | |
public class When_click_is_invalid : TrackingSpecificationContext | |
{ | |
Because of = () => | |
{ | |
validatorSvc | |
.Setup(m => m.Validate(click)) | |
.Returns(false); | |
sut.TrackClick(click); | |
}; | |
It should_not_check_balance = () => | |
balanceSvc.Verify(m => m.AccountHasBalance(AccountId), Times.Never()); | |
It should_not_save_click = () => | |
clickSvc.Verify(m => m.SaveTransaction(click), Times.Never()); | |
It should_throw_exception; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment