public void LinqToMocks()//IMPROVING MOCK SETUP READABILITY WITH LINQ TO MOCKS { //--------without LINQ-------------- //Mock<IFrequentFlyerNumberValidator> mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //var sut = new CreditCardApplicationEvaluator(mockValidator.Object); //-------------------------------------------------------------- IFrequentFlyerNumberValidator mockValidator = Mock.Of <IFrequentFlyerNumberValidator> ( validitor => validitor.ServiceInformation.License.LicenseKey == "OK" && validitor.IsValid(It.IsAny <string>()) == true ); var sut = new CreditCardApplicationEvaluator(mockValidator); var application = new CreditCardApplication { Age = 25 }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void DeclineLowIncomeApplications() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.IsValid("x")).Returns(true); //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //mockValidator.Setup( // x => x.IsValid(It.Is<string>(number => number.StartsWith("y")))) // .Returns(true); //mockValidator.Setup( // x => x.IsValid(It.IsInRange("a", "z", Moq.Range.Inclusive))) // .Returns(true); //mockValidator.Setup( // x => x.IsValid(It.IsIn("z", "y", "x"))) // .Returns(true); mockValidator.Setup( x => x.IsValid(It.IsRegex("[a-z]"))) .Returns(true); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "y" }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); } }
public void LinqToMocks() { //var mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); IFrequentFlyerNumberValidator mockValidator = Mock.Of <IFrequentFlyerNumberValidator> ( validator => validator.ServiceInformation.License.LicenseKey == "OK" && validator.IsValid(It.IsAny <string>()) == true ); var sut = new CreditCardApplicationEvaluator(mockValidator); var application = new CreditCardApplication { Age = 25 }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void DeclineLowIncomeApplications() { //Commented so we could use the one in the constructor //Mock<IFrequentFlyerNumberValidator> mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //setting the isvalid method // mockValidator.Setup(x => x.IsValid("x")).Returns(true); //Argument Matching //return true when any string is passed to the method //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //return true to a particular scenario // mockValidator.Setup(x => x.IsValid(It.Is<string>(numb=>numb.StartsWith("y")))).Returns(true); //return true if it is in a range //mockValidator.Setup(x => x.IsValid(It.IsInRange<string>("a","z",Moq.Range.Inclusive))).Returns(true); //return true if it is in a range //mockValidator.Setup(x => x.IsValid(It.IsIn<string>("z", "y", "x"))).Returns(true); //return true if it satisfies the regex mockValidator.Setup(x => x.IsValid(It.IsRegex("[a-z]"))).Returns(true); //var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "x" }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public async Task <IActionResult> Index(NewCreditCardApplicationDetails applicationDetails) { if (!ModelState.IsValid) { return(View(applicationDetails)); } var creditCardApplication = new CreditCardApplication { FirstName = applicationDetails.FirstName, LastName = applicationDetails.LastName, FrequentFlyerNumber = applicationDetails.FrequentFlyerNumber, Age = applicationDetails.Age.Value, GrossAnnualIncome = applicationDetails.GrossAnnualIncome.Value, RelationshipStatus = applicationDetails.RelationshipStatus, BusinessSource = applicationDetails.BusinessSource }; // There is no front-end validation required on the frequent flyer number. // If it is invalid the decision will be referred to a human. // This is so we don't miss out on potential sales due to people thinking // there frequent flyer number should be valid when it is not. var evaluator = new CreditCardApplicationEvaluator(new FrequentFlyerNumberValidator()); CreditCardApplicationDecision decision = evaluator.Evaluate(creditCardApplication); creditCardApplication.Decision = decision; await _applicationRepository.AddAsync(creditCardApplication); return(View("ApplicationComplete", creditCardApplication)); }
public void CreditCardApplication_ReferInvalidFreqFlyerApplication_SequenceTest() { // Example of setting up a sequence // Arrange var _application = new CreditCardApplication { Age = 25, FrequentFlyerNumber = "x" }; _frequentFlyerNumberValidator.Setup(x => x.ServiceInformation.Licence.LicenceKey) .Returns("Ok"); _frequentFlyerNumberValidator.SetupSet(x => x.ValidationMode = ValidationMode.Quick); _frequentFlyerNumberValidator.SetupSequence(x => x.IsValid(It.IsAny <string>())) .Throws <Exception>() .Returns(true); var _sut = new CreditCardApplicationEvaluator(_frequentFlyerNumberValidator.Object); // Act/Assert CreditCardApplicationDecision decision = _sut.Evaluate(_application); Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); CreditCardApplicationDecision decision2 = _sut.Evaluate(_application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision2); }
public void CreditCardApplication_LinqToMocks() { // Arrange //_frequentFlyerNumberValidator.Setup(x => x.IsValid(It.IsAny<string>())) // .Returns(true); //_frequentFlyerNumberValidator.Setup(x => x.ServiceInformation.Licence.LicenceKey) // .Returns("Valid"); //_frequentFlyerNumberValidator.SetupSet(x => x.ValidationMode = ValidationMode.Quick); IFrequentFlyerNumberValidator frequentFlyerNumberValidator = Mock.Of <IFrequentFlyerNumberValidator> ( validator => validator.ServiceInformation.Licence.LicenceKey == "OK" && validator.IsValid(It.IsAny <string>()) == true && validator.ValidationMode == ValidationMode.Detailed ); var _sut = new CreditCardApplicationEvaluator(frequentFlyerNumberValidator); var _application = new CreditCardApplication { GrossAnnualIncome = 19999m, Age = 25, FrequentFlyerNumber = "test" }; // Act CreditCardApplicationDecision decision = _sut.Evaluate(_application); // Assert Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void UseDetailedLookupForOlderApplications() { /* * By default MOCK properties DO NOT remember changes doing in the test process. * Ex: IFrequentFlyerNumberValidator has a enum properties, it is modified in the Evaluate Method * however when return from the method, the enum property do not keep the changed value. * This is de default behaviour but if we want to keep the changes to TRACKING DATA * we can two options: */ var mockValidator = new Mock <IFrequentFlyerNumberValidator>(); //TODO: Tracking changes to mock properties values - track all properties //SetupAllProperties - order is important, must be before Setup mockValidator.SetupAllProperties(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //TODO: Tracking changes to mock properties values - track a specified property ///mockValidator.SetupProperty(x => x.ValidationMode); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { Age = 30 }; CreditCardApplicationDecision decision = sut.Evaluate(application); //check value property - tracking changes of ValidationMode property Assert.Equal(ValidationMode.Detailed, mockValidator.Object.ValidationMode); }
public void ReferYoungApplications() { //Arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.IsValid(It.IsAny <string>())).Returns(true); /* * TODO: Specifyng DefaultValue strategy * With this approach, we write a explicit value for LicenseKey property * mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns(GetLicenseKeyExpiryString); * * With the following approach, we delegate to Moq the default value. * In this case: x.ServiceInformation.License.LicenseKey = null porque LicenseKey is STRING. * Si fuea una clase/interface seria mockeada. * * Si o si tenemos que usar uno de los dos approachs porque sino vamos a tener un NULL-REFERENCE-EXCEPTION */ mockValidator.DefaultValue = DefaultValue.Mock; var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { Age = 19 }; //Act CreditCardApplicationDecision decision = sut.Evaluate(application); //Assert Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }
public void DeclineLowIncomeApplicationsOutDemo() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(MockBehavior.Loose); // Using It class for Argument matching in mocked methods bool isValid = true; mockValidator.Setup(x => x.IsValid(It.IsAny <string>(), out isValid)); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "x" // use this if it.isIn,It.IsInRange or is.valid method // using It class from moq. }; CreditCardApplicationDecision decision = sut.EvaluateUsingOut(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void ReferInvalidFrequentFlyerApplications() { /* * Mock Behaviours * MockBehavior.Loose: (default) if we don't setup any methods, never thow exceptions, return default values of returning type. * MockBehavior.Strict: if we don't setup a method & then, this method is called --> throw an exception. */ /* * TODO: Mocking with MockBehavior.Strict */ //Arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(MockBehavior.Strict); //EXPLICIT Mocking mockValidator.Setup(x => x.IsValid(It.IsAny <string>())).Returns(false); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns(GetLicenseKeyExpiryString); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication(); //Act CreditCardApplicationDecision decision = sut.Evaluate(application); //Assert Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }
public void DeclineLowIncomeApplications() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(MockBehavior.Loose); // Using It class for Argument matching in mocked methods // mockValidator.Setup(x => x.IsValid("x")).Returns(true); // mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); // mockValidator.Setup(x => x.IsValid(It.IsIn<string>("x","y","z"))).Returns(true); // mockValidator.Setup(x => x.IsValid(It.IsInRange("x","y",Range.Inclusive))).Returns(true); mockValidator.Setup(x => x.IsValid(It.IsRegex("[a-z]", System.Text.RegularExpressions.RegexOptions.None))).Returns(true); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "x" // use this if it.isIn,It.IsInRange or is.valid method // using It class from moq. }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public async Task <IActionResult> Index(NewCreditCardApplicationDetails applicationDetails) { if (!ModelState.IsValid) { return(View(applicationDetails)); } var creditCardApplication = new CreditCardApplication { FirstName = applicationDetails.FirstName, LastName = applicationDetails.LastName, FrequentFlyerNumber = applicationDetails.FrequentFlyerNumber, Age = applicationDetails.Age.Value, GrossAnnualIncome = applicationDetails.GrossAnnualIncome.Value }; // Not mock-able var evaluator = new CreditCardApplicationEvaluator(new FrequentFlyerNumberValidator()); CreditCardApplicationDecision decision = evaluator.Evaluate(creditCardApplication); creditCardApplication.Decision = decision; await _applicationRepository.AddAsync(creditCardApplication); return(View("ApplicationComplete", creditCardApplication)); }
public void DeclineLowIncomeApplications() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.IsValid("y")).Returns(true);//returns true only when FrequentFlyerNumber = "x" //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true);//returns true regardless of FrequentFlyerNumber value //mockValidator.Setup(x => x.IsValid(It.Is<string>(number=>number.StartsWith("x")))).Returns(true);//returns true only if FrequentFlyerNumber value starts with "x" //mockValidator.Setup(x => x.IsValid(It.IsInRange<string>("a","z",Moq.Range.Inclusive))).Returns(true);//returns true only if FrequentFlyerNumber value is between "a" and "z" inclusive //mockValidator.Setup(x => x.IsValid(It.IsInRange("a", "z", Moq.Range.Inclusive))).Returns(true);//mentioning type is not necessary, it can automatically infer //mockValidator.Setup(x => x.IsValid(It.IsIn("z","y","x"))).Returns(true);//returns true only if FrequentFlyerNumber value is "z" or "y" or "x" mockValidator.Setup(x => x.IsValid(It.IsRegex("[a-z]"))).Returns(true);//returns true only if FrequentFlyerNumber value is in letters a to z mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK");//this code placed after hierarchy licensekey var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "x" }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); //MockBehaviour.Strict> throws exception if mocked method is called but not been setup //MockBehaviour.Loose> never throws exception if mocked method is called but not been setup.it returns default values for value types,null for reference types,empty for array/enumerable //MockBehaviour.Default> Default Behaviour if none is specified.(MockBehaviour.Loose) }
public void CreditCardApplication_RefersWhenLicenceKeyExpired() { // Arrange var _sut = new CreditCardApplicationEvaluator(_frequentFlyerNumberValidator.Object); var _application = new CreditCardApplication { GrossAnnualIncome = 19999m, Age = 25, FrequentFlyerNumber = "test" }; //var _mockLicenceData = new Mock<ILicenceData>(); //_mockLicenceData.Setup(x => x.LicenceKey).Returns("EXPIRED"); //var _mockServiceInfo = new Mock<IServiceInformation>(); //_mockServiceInfo.Setup(x => x.Licence).Returns(_mockLicenceData.Object); _frequentFlyerNumberValidator.Setup(x => x.IsValid(It.Is <string>(y => y.Contains("test")))) .Returns(true); _frequentFlyerNumberValidator.Setup(x => x.ServiceInformation.Licence.LicenceKey) .Returns("EXPIRED"); //_frequentFlyerNumberValidator.Setup(x => x.ServiceInformation).Returns(_mockServiceInfo.Object); // Act CreditCardApplicationDecision decision = _sut.Evaluate(_application); // Assert Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }
public void ReferInvalidFrequentFlyerApplications_Sequence() { //arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); /* * TODO: Returning different results for sequence calls */ mockValidator.SetupSequence(x => x.IsValid(It.IsAny <string>())) .Returns(false) .Returns(true); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { Age = 25 }; //act | assert CreditCardApplicationDecision firstDecision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, firstDecision); //act | assert CreditCardApplicationDecision secondDecision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, secondDecision); }
public void CreditCardApplication_IncrementLookupCountOnValidatorLookupPerformedEvent() { // Example of raising events on a moq object // Arrange var _application = new CreditCardApplication { Age = 25, FrequentFlyerNumber = "x" }; _frequentFlyerNumberValidator.Setup(x => x.IsValid(It.IsAny <string>())) .Returns(true) // Chain on the event being rasied from the setup. .Raises(x => x.ValidatorLookupPerformed += null, EventArgs.Empty); _frequentFlyerNumberValidator.Setup(x => x.ServiceInformation.Licence.LicenceKey) .Returns("Ok"); _frequentFlyerNumberValidator.SetupSet(x => x.ValidationMode = ValidationMode.Quick); var _sut = new CreditCardApplicationEvaluator(_frequentFlyerNumberValidator.Object); // Act CreditCardApplicationDecision decision = _sut.Evaluate(_application); // Manually invoke the event //_frequentFlyerNumberValidator.Raise(x => x.ValidatorLookupPerformed += null, EventArgs.Empty); // Assert Assert.Equal(1, _sut.ValidatorLookupCount); }
public void ReferFraudRisk() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); /* * TODO: Mocking members of a concrete class * Protected method & virtual methods */ Mock <FraudLookup> mockFraudLookup = new Mock <FraudLookup>(); //mockFraudLookup.Setup(x => x.IsFraudRisk(It.IsAny<CreditCardApplication>())) // .Returns(true); mockFraudLookup.Protected() .Setup <bool>("CheckApplication", ItExpr.IsAny <CreditCardApplication>()) .Returns(true); var sut = new CreditCardApplicationEvaluator(mockValidator.Object, mockFraudLookup.Object); var application = new CreditCardApplication(); CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.ReferredToHumanFraudRisk, decision); }
public void CreditCardApplication_ReferFraudRisk() { // Arrange var _application = new CreditCardApplication { GrossAnnualIncome = 35452m, Age = 20, FrequentFlyerNumber = "test" }; // On non interface test mocks, methods must be virtual in order to allow them to be overridden in the test. // This is why it is better to use Interfaces where possible. Mock <FraudLookup> mockFraudLookup = new Mock <FraudLookup>(); //mockFraudLookup.Setup(x => x.IsFraudRisk(It.IsAny<CreditCardApplication>())) // .Returns(true); // How to do the same with a protected method mockFraudLookup .Protected() .Setup <bool>("CheckApplication", ItExpr.IsAny <CreditCardApplication>()) .Returns(true); var _sut = new CreditCardApplicationEvaluator(_frequentFlyerNumberValidator.Object, mockFraudLookup.Object); // Act CreditCardApplicationDecision decision = _sut.Evaluate(_application); // Assert Assert.Equal(CreditCardApplicationDecision.ReferredToHumanFraudRisk, decision); }
public void DeclineLowIncomeApplications() { //Arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey); //RETURN NULL /* * TODO: Mock result Method - setup the mockValidator for the IsValid Method return true! * IsValid method will return TRUE when the parameter is "XXX1", if the FrequentFlyerNumber is * different to "XXX1", the IsValid Method will return FALSE */ mockValidator.Setup(x => x.IsValid("XXX1")).Returns(true); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "XXX1" //This value map with setup mock configuration }; //act CreditCardApplicationDecision decision = sut.Evaluate(application); //assert Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void ReferInvalidFrequentFlyerApplications_ReturnValuesSequence() { //Commented so we could use the one in the constructor //Mock<IFrequentFlyerNumberValidator> mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //will return several sequential value mockValidator.SetupSequence(x => x.IsValid(It.IsAny <string>())) .Returns(false) .Returns(true); //Commented so we could use the one in the constructor //var sut = new CreditCardApplicationEvaluator(mockValidator.Object); //var application = new CreditCardApplication(); var application = new CreditCardApplication { Age = 25 }; //first return value CreditCardApplicationDecision firstDecision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, firstDecision); //second return value CreditCardApplicationDecision secondDecision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, secondDecision); }
public void ReferWhenFrequentFlyerNumberValidationError() { //arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //generic version //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Throws<Exception>(); //non generic version mockValidator.Setup(x => x.IsValid(It.IsAny <string>())) .Throws(new Exception("Custom message")); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); CreditCardApplication application = new CreditCardApplication { Age = 42 }; //act CreditCardApplicationDecision decision = sut.Evaluate(application); //assert Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }
public void DeclineLowIncomeApplicationsOutDemo() { //-- Arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); bool isValid = true; mockValidator.Setup(x => x.IsValid(It.IsAny <string>(), out isValid)); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "a" }; //-- Act CreditCardApplicationDecision decision = sut.EvaluateUsingOut(application); //-- Assert Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); } // other evaluator test conditions }
public void DeclineLowIncomeApplications() { //arrange Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); //hardcoded //mockValidator.Setup(x => x.IsValid("x")).Returns(true); //it class for a more general result and methods //method for any string ever true even if empty //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //method for string that start with x //mockValidator.Setup(x => x.IsValid(It.Is<string>(number => number.StartsWith('x')))).Returns(true); //method for strings that contains x //mockValidator.Setup(x => x.IsValid(It.IsIn("x","y","z"))).Returns(true); //method for string what contains only letters //mockValidator.Setup(x => x.IsValid(It.IsInRange("a", "z", Range.Inclusive))).Returns(true); //method similar to regular validations mockValidator.Setup(x => x.IsValid(It.IsRegex("[a-z]", System.Text.RegularExpressions.RegexOptions.None))).Returns(true); //mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey); mockValidator.DefaultValue = DefaultValue.Mock; var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 10_000, Age = 42, FrequentFlyerNumber = "x" }; //act CreditCardApplicationDecision decision = sut.Evaluate(application); //assert Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void DeclineLowIncomeApplications() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //mockValidator.Setup(x => x.IsValid(It.Is<string>(number => number.StartsWith('x')))) // .Returns(true); //mockValidator.Setup(x => x.IsValid(It.IsIn("x", "y", "z"))).Returns(true); //mockValidator.Setup(x => x.IsValid(It.IsInRange("b", "z", Range.Inclusive))) // .Returns(true); mockValidator.Setup(x => x.IsValid(It.IsRegex("[a-z]", System.Text.RegularExpressions.RegexOptions.None))) .Returns(true); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, FrequentFlyerNumber = "a" }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void Decline_Low_Income_Applications() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); //It.IsAny<string>() mockValidator.Setup(x => x.IsValid("x")).Returns(true); //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //mockValidator.Setup(x => x.IsValid(It.Is<string>(number => number.StartsWith('x')))) // .Returns(true); //mockValidator.Setup(x => x.IsValid(It.IsIn("x", "y", "z"))).Returns(true); //mockValidator.Setup(x => x.IsValid(It.IsInRange("b", "z", Range.Inclusive))) // .Returns(true); //TODO: 06 - Seteo un comportamiento por defecto Mock mockValidator.DefaultValue = DefaultValue.Mock; var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 19_999, Age = 42, //Envío el parametro de frecuente que machea con el mock FrequentFlyerNumber = "x" }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void ReferWhenLicenseKeyExpired() { //var mockLicenseData = new Mock<ILicenseData>(); //mockLicenseData.Setup(x => x.LicenseKey).Returns("EXPIRED"); //var mockServiceInfo = new Mock<IServiceInformation>(); //mockServiceInfo.Setup(x => x.License).Returns(mockLicenseData.Object); //var mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.ServiceInformation).Returns(mockServiceInfo.Object); var mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("EXPIRED"); mockValidator.Setup(x => x.IsValid(It.IsAny <string>())).Returns(true); //mockValidator.Setup(x => x.LicenseKey).Returns(GetLicenseKeyExpiryString); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { Age = 42 }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }
public void CreditCardApplication_RefersWhenInvalidFreqFlyer() { // Arrange _frequentFlyerNumberValidator = new Mock <IFrequentFlyerNumberValidator>(); var _sut = new CreditCardApplicationEvaluator(_frequentFlyerNumberValidator.Object); var _application = new CreditCardApplication { GrossAnnualIncome = 25000m, Age = 25, FrequentFlyerNumber = "test4" }; // Argument array matching //_frequentFlyerNumberValidator.Setup(x => x.IsValid(It.IsIn("test", "test1", "test3"))) // .Returns(false); // Argument regex matching _frequentFlyerNumberValidator.Setup(x => x.IsValid(It.IsRegex("(?i:t)est"))) .Returns(false); // Returns default mock objects of Interfaces or abstract classes within a mocked object. _frequentFlyerNumberValidator.DefaultValue = DefaultValue.Mock; // Act CreditCardApplicationDecision decision = _sut.Evaluate(_application); // Assert Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }
public void DeclineLowIncomeApplications() { Mock <IFrequentFlyerNumberValidator> mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("OK"); //mockValidator.Setup(x => x.IsValid("x")).Returns(true); //if input is not "x", mock will return default, which is false //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //mockValidator.Setup(x => x.IsValid(It.Is<string>(number => number.StartsWith("y")))).Returns(true); //mockValidator.Setup( // x => x.IsValid(It.IsInRange<string>("a", "z", Moq.Range.Inclusive))) // .Returns(true); //mockValidator.Setup( // x => x.IsValid(It.IsIn<string>("a", "z", "y"))) // .Returns(true); mockValidator.Setup( x => x.IsValid(It.IsRegex("[a-z]"))) .Returns(true); var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { GrossAnnualIncome = 10_000, Age = 42, FrequentFlyerNumber = "y" }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.AutoDeclined, decision); }
public void ReferWhenLicenseKeyExpired()//CONFIGURING MOCKED PROPERTY TO RETURN A SPECIFIED VALUE { //var mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.IsValid(It.IsAny<string>())).Returns(true); //mockValidator.Setup(x => x.LicenseKey).Returns("EXPIRED"); OR //mockValidator.Setup(x => x.LicenseKey).Returns(GetLicenseKeyExpiryString);//GETTING A RETURN VALUE FROM A FUNCTION //-------------------AUTOMOCKING PROPERTY HIERARCHIES---------------------- //var mockLicenseData = new Mock<ILicenseData>(); //mockLicenseData.Setup(x=>x.LicenseKey).Returns("EXPIRED"); //var mockServiceInfo = new Mock<IServiceInformation>(); //mockServiceInfo.Setup(x => x.License).Returns(mockLicenseData.Object); //var mockValidator = new Mock<IFrequentFlyerNumberValidator>(); //mockValidator.Setup(x => x.ServiceInformation).Returns(mockServiceInfo.Object); //OR var mockValidator = new Mock <IFrequentFlyerNumberValidator>(); mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("EXPIRED"); //-------------------------------------------------------------------------- var sut = new CreditCardApplicationEvaluator(mockValidator.Object); var application = new CreditCardApplication { Age = 42 }; CreditCardApplicationDecision decision = sut.Evaluate(application); Assert.Equal(CreditCardApplicationDecision.ReferredToHuman, decision); }