public void TestCanRedeemCoupon() { var cm1 = new CouponManager(LogManager.GetCurrentClassLogger(), new CouponProvider()); var ex = Assert.CatchAsync <ArgumentNullException>(() => cm1.CanRedeemCoupon(Guid.NewGuid(), Guid.NewGuid(), null)); Assert.AreEqual(ex.ParamName, "evaluators"); var evaluators = new List <Func <CouponManager, Guid, bool> >(); Assert.Catch <KeyNotFoundException>(async() => await cm1.CanRedeemCoupon(Guid.Empty, Guid.NewGuid(), evaluators)); var xx = Guid.NewGuid(); Task <bool> r1; r1 = cm1.CanRedeemCoupon(xx, Guid.NewGuid(), evaluators); Assert.IsTrue(r1.Result); evaluators.Add((manager, guid) => true); r1 = cm1.CanRedeemCoupon(xx, Guid.NewGuid(), evaluators); Assert.IsTrue(r1.Result); evaluators.Add((manager, guid) => false); r1 = cm1.CanRedeemCoupon(xx, Guid.NewGuid(), evaluators); Assert.IsFalse(r1.Result); }
public void CouponManagerCanRedeemCouponTest_Evaluators() { var logger = new Mock <ILogger>(); var couponProvider = new Mock <ICouponProvider>(); var couponManager = new CouponManager(logger.Object, couponProvider.Object); couponProvider.Setup(s => s.Retrieve(It.IsAny <Guid>())).ReturnsAsync(new Coupon()); var evals = new List <Func <Coupon, Guid, bool> >() { (coupon, guid) => false, (coupon, guid) => false }; var result = couponManager.CanRedeemCoupon(Guid.Empty, Guid.Empty, evals).Result; Assert.IsFalse(result); evals = new List <Func <Coupon, Guid, bool> >() { (coupon, guid) => true, (coupon, guid) => false }; var result1 = couponManager.CanRedeemCoupon(Guid.Empty, Guid.Empty, evals).Result; Assert.IsTrue(result1); }
public void CanRedeemCoupon_Should_Not_Accept_Null_Valuators() { //Arrange var couponId = Guid.NewGuid(); var userId = Guid.NewGuid(); // Assert Assert.ThrowsAsync <ArgumentNullException>(() => couponManager.CanRedeemCoupon(couponId, userId, null)); }
public void CanRedeemCoupon_throw_argument_null_exception_when_evaluators_is_null() { var couponId = Guid.NewGuid(); var userId = Guid.NewGuid(); IEnumerable <Func <Coupon, Guid, bool> > evaluators = null; var actual = Assert.ThrowsExceptionAsync <ArgumentNullException>(() => _manager.CanRedeemCoupon(couponId, userId, evaluators)); Assert.IsNotNull(actual); Assert.AreEqual(nameof(evaluators), actual.Result.ParamName); }
public void TestCase3_CheckEvaluatorIsNull() { // Setup MyLogger log = new MyLogger(); MyCouponProvider cp = new MyCouponProvider(); Guid couponId = new Guid("00000000-0000-0000-0000-00000000BA12"); Guid userId = new Guid("00000000-0000-0000-0000-000000001111"); List <Func <Coupon, Guid, bool> > theList = null; CouponManager cm = new CouponManager(log, cp); try { // Apply Task <bool> task = cm.CanRedeemCoupon(couponId, userId, theList); task.Wait(); bool result = task.Result; // Check Assert.Fail("Exception must be thrown"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("evaluators")); } }
public void TestCase2_CheckFalse() { // Setup MyLogger log = new MyLogger(); MyCouponProvider cp = new MyCouponProvider(); Guid couponId = new Guid("00000000-0000-0000-0000-00000000BA12"); Guid userId = new Guid("00000000-0000-0000-0000-000000001111"); List <Func <Coupon, Guid, bool> > theList = new List <Func <Coupon, Guid, bool> >(); for (int i = 0; i < 10; i++) { CouponGuid temp = new CouponGuid(); theList.Add(temp.Evaluate); } CouponManager cm = new CouponManager(log, cp); // Apply try { Task <bool> task = cm.CanRedeemCoupon(couponId, userId, theList); task.Wait(); bool result = task.Result; // Check Assert.AreEqual(result, false); } catch (Exception e) { Assert.Fail(e.Message); } }
public void TestCase4_CheckCouponIsNull() { // Setup MyLogger log = new MyLogger(); MyCouponProvider cp = new MyCouponProvider(); Guid couponId = new Guid("00000000-0000-0000-0000-000000002222"); Guid userId = new Guid("00000000-0000-0000-0000-000000001111"); List <Func <Coupon, Guid, bool> > theList = new List <Func <Coupon, Guid, bool> >(); for (int i = 0; i < 10; i++) { CouponGuid temp = new CouponGuid(); theList.Add(temp.Evaluate); } CouponManager cm = new CouponManager(log, cp); try { // Apply Task <bool> task = cm.CanRedeemCoupon(couponId, userId, theList); task.Wait(); bool result = task.Result; // Check Assert.Fail("Exception must be thrown"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("The given key was not present in the dictionary")); } }
public async Task Arguments_are_validated_for_CanRedeemCoupon_method() { var mockLogger = Mock.Of <ILogger>(); var mockProvider = Mock.Of <ICouponProvider>(); var sut = new CouponManager(mockLogger, mockProvider); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CanRedeemCoupon(Guid.NewGuid(), Guid.NewGuid(), null)); }
public void TestCase5_CheckEvaluatorEmpty() { // Setup MyLogger log = new MyLogger(); MyCouponProvider cp = new MyCouponProvider(); Guid couponId = new Guid("00000000-0000-0000-0000-00000000BBBB"); Guid userId = new Guid("00000000-0000-0000-0000-00000000ABAB"); List <Func <Coupon, Guid, bool> > theList = new List <Func <Coupon, Guid, bool> >(); CouponManager cm = new CouponManager(log, cp); try { // Apply Task <bool> task = cm.CanRedeemCoupon(couponId, userId, theList); task.Wait(); bool result = task.Result; // Check Assert.AreEqual(result, true); } catch (Exception e) { Assert.Fail(e.Message); } }
public void CouponManagerCanRedeemCouponTest_ArgumentNull() { var logger = new Mock <ILogger>(); var couponProvider = new Mock <ICouponProvider>(); var couponManager = new CouponManager(logger.Object, couponProvider.Object); Assert.ThrowsAsync <ArgumentNullException>(async() => await couponManager.CanRedeemCoupon(Guid.Empty, Guid.Empty, null)); }
public void CouponManagerCanRedeemCouponTest_CouponNotFound() { var logger = new Mock <ILogger>(); var couponProvider = new Mock <ICouponProvider>(); var couponManager = new CouponManager(logger.Object, couponProvider.Object); var evals = new List <Func <Coupon, Guid, bool> >(); Assert.ThrowsAsync <KeyNotFoundException>(async() => await couponManager.CanRedeemCoupon(Guid.Empty, Guid.Empty, evals)); }
public async Task CanRedeemCoupon_Should_ThrowException_When_Evaluators_Is_Null() { //Arrange Mock <ILogger> logger = new Mock <ILogger>(); Mock <ICouponProvider> couponProvider = new Mock <ICouponProvider>(); //Assign var couponManager = new CouponManager(logger.Object, couponProvider.Object); var couponGuid = Guid.NewGuid(); var userId = Guid.NewGuid(); var exception = await Assert.ThrowsAsync <ArgumentNullException>(() => couponManager.CanRedeemCoupon(couponGuid, userId, null)); //Assert Assert.Equal("Value cannot be null. (Parameter 'evaluators')", exception.Message); }
public async Task Throw_Exception_When_Evaluators_Null() { //Arrange var mockLogger = new Mock <ILogger>(); var mockCouponProvider = new Mock <ICouponProvider>(); var couponId = new Guid(); var userId = new Guid(); var manager = new CouponManager(mockLogger.Object, mockCouponProvider.Object); IEnumerable <Func <Coupon, Guid, bool> > evaluators = null; //Act var exception = await Assert.ThrowsAsync <ArgumentNullException>(() => manager.CanRedeemCoupon(couponId, userId, evaluators)); //Assert Assert.Equal(nameof(evaluators), exception.ParamName); }
public async Task Throw_Exception_When_Retrieved_Coupon_Null() { //Arrange var couponId = new Guid(); var userId = new Guid(); var mockLogger = new Mock <ILogger>(); var mockEvaluators = new List <Func <Coupon, Guid, bool> >(); var mockCouponProvider = new Mock <ICouponProvider>(); mockCouponProvider .Setup(provider => provider.Retrieve(couponId)) .ReturnsAsync(() => null); var manager = new CouponManager(mockLogger.Object, mockCouponProvider.Object); //Act Assert await Assert.ThrowsAsync <KeyNotFoundException>(() => manager.CanRedeemCoupon(couponId, userId, mockEvaluators)); }
public async Task CanRedeemCoupon_Should_ThrowException_When_Coupon_Is_Null() { //Arrange Mock <ICouponProvider> couponProvider = new Mock <ICouponProvider>(); Mock <IEnumerable <Func <Coupon, Guid, bool> > > evaluators = new Mock <IEnumerable <Func <Coupon, Guid, bool> > >(); Mock <ILogger> logger = new Mock <ILogger>(); couponProvider.Setup(x => x.Retrieve(It.IsAny <Guid>())) .Returns(Task.FromResult <Coupon>(null)); //Assign var couponManager = new CouponManager(logger.Object, couponProvider.Object); var couponGuid = Guid.NewGuid(); var userId = Guid.NewGuid(); //Assert await Assert.ThrowsAsync <KeyNotFoundException>(() => couponManager.CanRedeemCoupon(couponGuid, userId, evaluators.Object)); }
public async Task CanRedeemCoupon_Should_Return_True_When_No_Evaluators() { //Arrange Mock <ICouponProvider> couponProvider = new Mock <ICouponProvider>(); couponProvider.Setup(x => x.Retrieve(It.IsAny <Guid>())) .Returns(Task.FromResult(new Coupon())); IEnumerable <Func <Coupon, Guid, bool> > evaluators = new List <Func <Coupon, Guid, bool> >(); Mock <ILogger> logger = new Mock <ILogger>(); //Assign var couponManager = new CouponManager(logger.Object, couponProvider.Object); var couponGuid = Guid.NewGuid(); var userId = Guid.NewGuid(); var canRedeemCoupon = await couponManager.CanRedeemCoupon(couponGuid, userId, evaluators); //Assert Assert.True(canRedeemCoupon); }
public async Task Return_True_When_Evaluators_Empty() { //Arrange var couponId = new Guid(); var userId = new Guid(); var mockLogger = new Mock <ILogger>(); var mockEvaluators = new List <Func <Coupon, Guid, bool> >(); var mockCouponProvider = new Mock <ICouponProvider>(); mockCouponProvider .Setup(provider => provider.Retrieve(couponId)) .ReturnsAsync(() => new Coupon()); var manager = new CouponManager(mockLogger.Object, mockCouponProvider.Object); //Act var result = await manager.CanRedeemCoupon(couponId, userId, mockEvaluators); //Assert Assert.True(result); }
public async Task CanRedeemCoupon_Should_Return_False_For_Falsey_Evaluation() { //Arrange Mock <ICouponProvider> couponProvider = new Mock <ICouponProvider>(); couponProvider.Setup(x => x.Retrieve(It.IsAny <Guid>())) .Returns(Task.FromResult(new Coupon())); List <Func <Coupon, Guid, bool> > evaluators = new List <Func <Coupon, Guid, bool> >(); evaluators.Add((Coupon, Guid) => { return(false); }); Mock <ILogger> logger = new Mock <ILogger>(); //Assign var couponManager = new CouponManager(logger.Object, couponProvider.Object); var couponGuid = Guid.NewGuid(); var userId = Guid.NewGuid(); var canRedeemCoupon = await couponManager.CanRedeemCoupon(couponGuid, userId, evaluators); //Assert Assert.False(canRedeemCoupon); }
public async Task Return_False_When_All_Evaluator_Fail() { //Arrange var couponId = new Guid(); var userId = new Guid(); var mockLogger = new Mock <ILogger>(); bool Rfalse(Coupon coupon, Guid guid) { return(false); } bool Rfalse2(Coupon coupon, Guid guid) { return(false); } var mockEvaluators = new List <Func <Coupon, Guid, bool> >() { Rfalse, Rfalse2 }; var mockCouponProvider = new Mock <ICouponProvider>(); mockCouponProvider .Setup(provider => provider.Retrieve(couponId)) .ReturnsAsync(() => new Coupon()); var manager = new CouponManager(mockLogger.Object, mockCouponProvider.Object); //Act var result = await manager.CanRedeemCoupon(couponId, userId, mockEvaluators); //Assert Assert.False(result); }