Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active August 29, 2015 14:22
Show Gist options
  • Save christopherbauer/915c6043f8d19d9d631f to your computer and use it in GitHub Desktop.
Save christopherbauer/915c6043f8d19d9d631f to your computer and use it in GitHub Desktop.
Mocking IHttpRequestContext for Unit Testing
[TestFixture]
public class when_removing_a_coupon
{
[Test]
public void then_should_remove_the_coupon_from_the_session()
{
// Arrange
var httpSessionStateBase = new Mock<HttpSessionStateBase>();
var coupons = new Dictionary<int, Coupon>();
httpSessionStateBase.SetupGet(session => session["PendingBuyerCoupons"])
.Returns(buyerCoupons);
var httpRequestContext = new Mock<IHttpRequestContext>();
httpRequestContext.SetupGet(context => context.Session).Returns(httpSessionStateBase.Object);
var service = new SessionDataService(httpRequestContext.Object);
var coupon = new Coupon {DigitalCouponId = 1};
service.AddCoupon(coupon);
// Act
service.RemoveCoupon(coupon);
// Assert
Assert.That(coupons.Count, Is.EqualTo(0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment