public void CalculateScore() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); mockCreditScorer.Verify(x => x.CalculateScore( "Sarah", "133 Pluralsight Drive, Draper, Utah"), Times.Once); }
public void DeclineWhenCreditScoreError() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); mockCreditScorer.Setup(x => x.CalculateScore(It.IsAny <string>(), It.IsAny <string>())) .Throws(new InvalidOperationException("Test Exception")); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.False); }
public void InitializeIdentityVerifier() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); mockIdentityVerifier.Verify(x => x.Initialize()); mockIdentityVerifier.Verify(x => x.Validate(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>())); mockIdentityVerifier.VerifyNoOtherCalls(); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); }
public void AcceptApplication() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); // Mock object configuration is needed in this case. var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); // Moq can automatically create hierarchies of mocks without setup individual items with a default value being setted in the mock instantiation. //var mockCreditScorer = new Mock<ICreditScorer> { DefaultValue = DefaultValue.Mock }; // 1a. Instead of setting a literal value for an argument, It class specifies different ways that can match with the arguments values from an class implementation. // Doesn't check if the right arguments are being passed. //mockIdentityVerifier.Setup(x => x.Validate(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>())).Returns(true); // 1b. Mock using literal parameter values setting a specific method for configuration and testing. mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")).Returns(true); // 2. Mock with an output parameter configuration. Needs to add a proper param configuration for the specific overload of the interface method. //bool isValidOutValue = true; //mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah", out isValidOutValue)); // 3. Mock with a ref parameter configuration with callback that calls in conjunction with a delegate validate replacement method with by-ref parameters, specifying which param should be returned. //mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah", ref It.Ref<IdentityVerificationStatus>.IsAny)) // .Callback(new ValidateCallback( // (string applicantName, int applicantAge, string applicantAddress, ref IdentityVerificationStatus status) => // status = new IdentityVerificationStatus(true))); // 1. Basic setup for one level of hierarchy //mockCreditScorer.Setup(x => x.Score).Returns(300); // 2. Manual mock up setup for multiple levels of property hierarchy, going through each level and setting their mock objects with the previous cascading mock. //var mockScoreValue = new Mock<ScoreValue>(); //var mockScoreResult = new Mock<ScoreResult>(); //mockScoreValue.Setup(x => x.Score).Returns(300); //mockScoreResult.Setup(x => x.ScoreValue).Returns(mockScoreValue.Object); //mockCreditScorer.Setup(x => x.ScoreResult).Returns(mockScoreResult.Object); // As specific properties can be setted up, a list of all properties can be setted in a batch as well. Attention: it overrides any setups done previously. mockCreditScorer.SetupAllProperties(); // 3. Automatic mockup setup for multiple levels of property hierarchy (i.e. Moq can do it in its implementations AS LONG AS INNER PROPERTIES CAN BE OVERRIDDEN WITH A VIRTUAL DEFINITION). mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); // Certain properties from a mock can be setted up individually (e.g. property to track quantity of changes made). //mockCreditScorer.SetupProperty(x => x.Count); // System under test instance var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void NotAllowNullApplicationToBeProcessed() { var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); Assert.That(() => sut.Process(null), Throws.TypeOf <ArgumentNullException>()); }
public void AcceptUsingPartialMock() { var product = new LoanProduct(99, "Loan", 5.25m); var amount = new LoanAmount("USD", 200_000); var application = new LoanApplication( 42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IdentityVerifierServiceGateway>(); mockIdentityVerifier.Protected() .As <IIdentityVerifierServiceGatewayProtectedMembers>() .Setup(x => x.CallService(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>())) .Returns(true); var expectedTime = new DateTime(2000, 1, 1); mockIdentityVerifier.Protected() .As <IIdentityVerifierServiceGatewayProtectedMembers>() .Setup(x => x.GetCurrentTime()) .Returns(expectedTime); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); // Assert.That(mockIdentityVerifier.Object.LastCheckTime, Is.EqualTo(expectedTime)); mockIdentityVerifier.Object.LastCheckTime.Should().Be(expectedTime); mockIdentityVerifier.Object.LastCheckTime.Should().BeOnOrAfter(expectedTime); mockIdentityVerifier.Object.LastCheckTime.Should().BeOnOrBefore(expectedTime); mockIdentityVerifier.Object.LastCheckTime.Should().Be(1.January(2000)); mockIdentityVerifier.Object.LastCheckTime.Should().Be(1.January(2000).At(0, 0)); mockIdentityVerifier.Object.LastCheckTime.Should() .Be(1.Days().Before(2.January(2000))); mockIdentityVerifier.Object.LastCheckTime.Should().HaveYear(2000); mockIdentityVerifier.Object.LastCheckTime.Should().HaveDay(1); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication( 42, product, amount, "Sarah", 25, "133 Pluralsight Dr., Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); //In cases where we don't care what parameters are being sent, we can use MOQ's //It.IsAny for parameter substitution. But in this case, since we know what parameters // we are setting up our LoanApplication//with, it would be better to just use them in this case mockIdentityVerifier.Setup(x => x.Validate( "Sarah", 25, "133 Pluralsight Dr., Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); //The following sets up the property hierarchy to return a valid credit score mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //The following tells MOQ to track changes to the Count property so that we can test //it in the second assertion mockCreditScorer.SetupProperty(x => x.Count); //You can also automatically configure all properties for an object using //mockCreditScorer.SetupAllProperties(); //instead of the SetupProperty method. //However, the caveat here is that the above line would need to be placed above //the Setup method because having it below would REconfigure our explicitly set //ScoreResult.ScoreValue.Score of 300 var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process((application)); //Behavior-based testing //Here we can Verify the "Getter" property was accessed during this test mockCreditScorer.VerifyGet(x => x.ScoreResult.ScoreValue.Score, Times.Once); //And the set: mockCreditScorer.VerifySet(x => x.Count = 1); //Object-state testing Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void AcceptUsingPartialMock() { LoanProduct product = new(99, "Loan", 5.25m); LoanAmount amount = new("USD", 200_000); LoanApplication application = new(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IdentityVerifierServiceGateway>(); //mockIdentityVerifier // .Protected() // .Setup<bool>("CallService", // "Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah") // .Returns(true); mockIdentityVerifier .Protected() .As <IIdentityVerifierServieGetewayProtectedMember>() .Setup(x => x.CallService( "Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var expectedTime = new DateTime(2000, 1, 1); mockIdentityVerifier .Protected() .Setup <DateTime>("GetCurrentTime") .Returns(expectedTime); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer .Setup(x => x.ScoreResult.ScoreValue.Score) .Returns(300); //system under test var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockIdentityVerifier.Object.LastCheckTime, Is.EqualTo(expectedTime)); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); LoanApplication application = new LoanApplication(42, product, amount, "Bruno", 31, "Mullerstr. 3, Germany", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); // Setup mock with paremeters and returns. //mockIdentityVerifier.Setup(x => x.Validate("Bruno", 31, "Mullerstr. 3, Germany")).Returns(true); // Setup mock with out parameter. //bool isValidOutValue = true; //mockIdentityVerifier.Setup(x => x.Validate("Bruno", // 31, // "Mullerstr. 3, Germany", // out isValidOutValue)); // Setup mock with a ref parameter. mockIdentityVerifier.Setup(x => x.Validate("Bruno", 31, "Mullerstr. 3, Germany", ref It.Ref <IdentityVerificationStatus> .IsAny)) .Callback(new ValidateCallback( (string applicantName, int applicantAge, string applicantAddress, ref IdentityVerificationStatus status) => status = new IdentityVerificationStatus(true))); //// Using the class 'It', you can specify a specific condition to match a field. //// In the case below, the method will pass the tests independent of the typed values on fields. //// That's recommend to be used only when the value of the parameter is non-deterministic //mockIdentityVerifier.Setup(x => x.Validate( // It.IsAny<string>(), // It.IsAny<int>(), // It.IsAny<string>())) // .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); //mockIdentityVerifier.Setup(x => x.Validate(It.IsAny<string>(), // It.IsAny<int>(), // It.IsAny<string>())) // .Returns(true); //mockIdentityVerifier.Setup(x => x.Validate("Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah")) // .Returns(true); //bool isValidOutValue = true; //mockIdentityVerifier.Setup(x => x.Validate("Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah", // out isValidOutValue)); mockIdentityVerifier .Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah", ref It.Ref <IdentityVerificationStatus> .IsAny)) .Callback(new ValidateCallback( (string applicantName, int applicantAge, string applicantAddress, ref IdentityVerificationStatus status) => status = new IdentityVerificationStatus(true))); var mockCreditScorer = new Mock <ICreditScorer>(); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); }
public void DeclineLowSalary() { var loanProduct = new LoanProduct(1, "Loan", 8.35m); var loanAmount = new LoanAmount("inr", 3500000); var loanApplication = new LoanApplication(1, loanProduct, loanAmount, "Vatan", 35, "Ekta Nagar", 50000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); var lap = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); lap.Process(loanApplication); //It fails as LoanApplicationProcessor dont accept Null arguments. Assert.That(loanApplication.GetIsAccepted(), Is.False); }
public void DeclineLowSalary() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 64_999); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.False); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); // setting up a mock method return var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate(It.IsAny <string>(), It.IsAny <int>(), "133 Pluralsight Drive, Draper, Utah")).Returns(true); // setting up a mock property var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.Score).Returns(300); // manully setting up a mock property hierarchy // CreditScorer -> ScoreResult -> ScoreValue //var mockScoreValue = new Mock<ScoreValue>(); //mockScoreValue.Setup(x => x.Score).Returns(300); //var mockScoreResult = new Mock<ScoreResult>(); //mockScoreResult.Setup(x => x.ScoreValue).Returns(mockScoreValue.Object); //mockCreditScorer.Setup(x => x.ScoreResult).Returns(mockScoreResult.Object); // Getting moq to create the hierarchy of object // Moq creates each of the objects in the hierarchy, all the properties in hierarchy must be virtual mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); // Tracking changes to properties // Can provide an initial value with a second argument mockCreditScorer.SetupProperty(x => x.Count); // Can use the below to setup all properties but it will overwrite any previous setup, so use it first! //mockCreditScorer.SetupAllProperties(); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); // Check property was accessed, number of times is optional mockCreditScorer.VerifyGet(x => x.ScoreResult.ScoreValue.Score, Times.Once); // Check property was set mockCreditScorer.VerifySet(x => x.Count = It.IsAny <int>()); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); // can use It method if you don't need to check against specific values //mockIdentityVerifier.Setup(x => x.Validate(It.IsAny<string>(), // It.IsAny<int>(), // It.IsAny<string>())) // .Returns(true); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); // { DefaultValue = DefaultValue.Mock }; mock will automatically set up hierarchy mockCreditScorer.SetupAllProperties(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //mockCreditScorer.SetupProperty(x => x.Count); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); mockCreditScorer.VerifyGet(x => x.ScoreResult.ScoreValue.Score); // mockCreditScorer.VerifySet(x => x.Count = 1); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void AcceptUsingPartialMock() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var expectedTime = new DateTime(2000, 1, 1); var mockNowProvider = new Mock <INowProvider>(); mockNowProvider.Setup(x => x.GetNow()).Returns(expectedTime); var mockIdentityVerifier = new Mock <IdentityVerifierServiceGateway>(mockNowProvider.Object); // mockIdentityVerifier.Protected().Setup<bool>("CallService", // "Sarah",25,"address") // .Returns(true); mockIdentityVerifier.Protected() .As <IIdentityVerifierServiceGatewayProtectedMembers>() .Setup(x => x.CallService(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>())) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockIdentityVerifier.Object.LastCheckTime, Is.EqualTo(expectedTime)); }
public void DeclineLowSalary() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 64_999); var sut = new LoanApplicationProcessor(null, null); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.False); }
public void InitializeIdentityVerifier() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication( 42, product, amount, "Sarah", 25, "133 Pluralsight Dr., Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate( "Sarah", 25, "133 Pluralsight Dr., Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.SetupAllProperties(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process((application)); //Behavior-based testing //Verify is a method from MOQ that in this case will tell us if the Initialize method //on the mock IIdentityVerifier we have below has been called. mockIdentityVerifier.Verify(x => x.Initialize()); mockIdentityVerifier.Verify(x => x.Validate(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>())); //Here we can be very strict about what we expect to happen. After Verifying every //call that happens during testing, we can Verify that NOTHING else occurred. mockIdentityVerifier.VerifyNoOtherCalls(); }
public void CalculateScore() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication( 42, product, amount, "Sarah", 25, "133 Pluralsight Dr., Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate( "Sarah", 25, "133 Pluralsight Dr., Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.SetupAllProperties(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process((application)); //Behavior-based testing //As seen in previous examples but deleted now, we can use MOQ's It.IsAny<> to test cases where we want to check //if parameters were sent but don't particularly care what was sent mockCreditScorer.Verify(x => x.CalculateScore(It.IsAny <string>(), It.IsAny <string>())); //Here we are Verifying that CalculateScore was called with specific parameters, and ensuring that call //happened only once. mockCreditScorer.Verify(x => x.CalculateScore( "Sarah", "133 Pluralsight Dr., Draper, Utah"), Times.Once); }
public void AcceptUsingPartialMock() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); // We are using the actual class rather than the interface so the test can use real methods // We can also mock certain methods of the actual class, this is useful if the method interacts with external resource var mockIdentityVerifier = new Mock <IdentityVerifierServiceGateway>(); // Had to make method public and virtual // This setup works for public virtual methods //mockIdentityVerifier.Setup(x => x.CallService("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")).Returns(true); // For protected methods mockIdentityVerifier.Protected().Setup <bool>("CallService", "Sarah", 25, "133 Pluralsight Drive, Draper, Utah").Returns(true); var expectedTime = new DateTime(2000, 1, 1); // This setup works for public virtual methods //mockIdentityVerifier.Setup(x => x.GetCurrentTime()) // .Returns(expectedTime); // For protected method mockIdentityVerifier.Protected().Setup <DateTime>("GetCurrentTime") .Returns(expectedTime); // Note: to get intelisense for when mocking protected methods we need to define an interface and chain As method extension // this makes it possible to use a lamba extension // You can also refactor your code to extract methods var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockIdentityVerifier.Object.LastCheckTime, Is.EqualTo(expectedTime)); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(MockBehavior.Strict); mockIdentityVerifier.Setup(x => x.Initialize()); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.SetupAllProperties(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //mockCreditScorer.SetupProperty(x => x.Count); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); mockCreditScorer.VerifyGet(x => x.ScoreResult.ScoreValue.Score, Times.Once); //mockCreditScorer.VerifySet(x => x.Count = It.IsAny<int>(), Times.Once); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void DeclineLowSalary() { LoanProduct product = new LoanProduct(99, "Load", 5.25M); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Fake Dr", 64_999); // how to isolate the dependencies? Passing nulls cause this to fail. var sut = new LoanApplicationProcessor(null, null); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.False); }
public void AcceptUsingPartialMock() { // Arrange LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IdentityVerifierServiceGateway>(); mockIdentityVerifier .Protected() .Setup <bool>("CallService", "Sarah", 25, "133 Pluralsight Drive, Draper, Utah") .Returns(true); var expectedTime = new DateTime(2000, 1, 1); mockIdentityVerifier .Protected() .Setup <DateTime>("GetCurrentTime") .Returns(expectedTime); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer .Setup(x => x.Score) .Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); // Act sut.Process(application); // Assert Assert.That(application.GetIsAccepted(), Is.True); Assert.AreEqual(mockIdentityVerifier.Object.LastCheckTime, expectedTime); }
public void DeclineLowSalary() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); LoanApplication application = new LoanApplication(42, product, amount, "Bruno", 31, "Mullerstr. 3, Germany", 64_999); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.False); }
public void InitializeIdentity() { LoanProduct product = new(99, "Loan", 5.25m); LoanAmount amount = new("USD", 200_000); LoanApplication application = new(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier .Setup(x => x.Validate( "Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.SetupAllProperties(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //system under test var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); //veryfing that method (without parameters) was called/exeucted at least once mockIdentityVerifier.Verify(x => x.Initialize()); mockIdentityVerifier.Verify(x => x.Validate(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>())); mockIdentityVerifier.VerifyNoOtherCalls(); }
public void AcceptMinimumSalary() { LoanProduct product = new LoanProduct(99, "Load", 5.25M); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Fake Dr", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); // how to isolate the dependencies? Passing nulls cause this to fail. var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted(), Is.True); }
public void CalculateScore() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); var mockCreditScorer = new Mock <ICreditScorer>(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); // Verfying a method where parameters was called // mockCreditScorer.Verify(x => x.CalculateScore(It.IsAny<string>(), It.IsAny<string>())); mockCreditScorer.Verify(x => x.CalculateScore("Sarah", "133 Pluralsight Drive, Draper, Utah")); // Verifying a method was called a specific number of times mockCreditScorer.Verify(x => x.CalculateScore("Sarah", "133 Pluralsight Drive, Draper, Utah"), Times.Once); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "123 Main St Love, Anywhere USA", 65_000); // Instantiate Mock objects. var mockIdentityVerifier = new Mock <IIdentityVerifier>(); var mockCreditScorer = new Mock <ICreditScorer>(); var mockScoreValue = new Mock <ScoreValue>(); var mockScoreResult = new Mock <ScoreResult>(); // Pass in values to method Validate(p1, p2, p3). mockIdentityVerifier .Setup(x => x.Validate ("Sarah", 25, "123 Main St Love, Anywhere USA")) .Returns(true); mockCreditScorer.SetupAllProperties(); mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //mockCreditScorer.SetupProperty(x => x.Count); // Pass in IsAny<> for parameter replacement. //mockIdentityVerifier // .Setup(x => x.Validate // (It.IsAny<string>(), // It.IsAny<int>(), // It.IsAny<string>())) // .Returns(true); // Pass in values to method Validate(p1, p2, p3, out p4) using out parameter. //bool isValidOutValue = true; //mockIdentityVerifier // .Setup(x => x.Validate // ("Sarah", // 25, // "123 Main St Love, Anywhere USA", // out isValidOutValue)); // Pass in values to method Validate(p1, p2, p3, ref p4) using reference parameter // and CallBack delegate. Ugh. //mockIdentityVerifier // .Setup(x => x.Validate // ("Sarah", // 25, // "123 Main St Love, Anywhere USA", // ref It.Ref<IdentityVerificationStatus>.IsAny)) // .Callback(new ValidateCallback( // (string applicantName, // int applicantAge, // string applicantAddress, // ref IdentityVerificationStatus status) => // status = new IdentityVerificationStatus(true))); // Pass in Mock parameters. var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); Assert.That(application.GetIsAccepted, Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void Accept() { LoanProduct product = new LoanProduct(99, "Loan", 5.25m); LoanAmount amount = new LoanAmount("USD", 200_000); var application = new LoanApplication(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); //General mockIdentityVerifier.Setup(x => x.Validate("Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); //mockIdentityVerifier.Setup(x => x.Validate(It.IsAny<string>(), // It.IsAny<int>(), // It.IsAny<string>())) // .Returns(true); // With Out type //bool isValidOutValue = true; //mockIdentityVerifier.Setup(x => x.Validate("Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah", // out isValidOutValue)); // With Ref type //mockIdentityVerifier // .Setup(x => x.Validate("Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah", // ref It.Ref<IdentityVerificationStatus>.IsAny)) // .Callback(new ValidateCallback( // (string applicantName, // int applicantAge, // string applicantAddress, // ref IdentityVerificationStatus status) => // status = new IdentityVerificationStatus(true))); var mockCreditScorer = new Mock <ICreditScorer>(); // Track all properties changes // mockCreditScore.SetupAllProperties(); // mockCreditScorer.Setup(x => x.Score).Returns(300); // Mock Properties to Track changes // mockCreditScorer.SetupProperty(x => x.Count, initial value); mockCreditScorer.SetupProperty(x => x.Count); //Manullay mocking hierarchies //var mockScoreValue = new Mock<ScoreValue>(); //mockScoreValue.Setup(x => x.Score).Returns(300); //var mockScoreResult = new Mock<ScoreResult>(); //mockScoreResult.Setup(x => x.ScoreValue).Returns(mockScoreValue.Object); //mockCreditScorer.Setup(x => x.ScoreResult).Returns(mockScoreResult.Object); //Auto Mocking hierarchies provided all properties are in virtual mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //Auto Mocking hierarchies method 2 //var mockCreditScorer = new Mock<ICreditScorer> { DefaultValue = DefaultValue.Mock}; or { DefaultValue = DefaultValue.Empty} //mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); // Verfying property setter and getter were called mockCreditScorer.VerifyGet(x => x.ScoreResult.ScoreValue.Score, Times.Once); mockCreditScorer.VerifySet(x => x.Count = It.IsAny <int>(), Times.Once); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }
public void Accept() { LoanProduct product = new(99, "Loan", 5.25m); LoanAmount amount = new("USD", 200_000); LoanApplication application = new(42, product, amount, "Sarah", 25, "133 Pluralsight Drive, Draper, Utah", 65_000); var mockIdentityVerifier = new Mock <IIdentityVerifier>(); //Configuring mock object method return values mockIdentityVerifier .Setup(x => x.Validate( "Sarah", 25, "133 Pluralsight Drive, Draper, Utah")) .Returns(true); //Argument matching in mocked methods //mockIdentityVerifier // .Setup(x => x.Validate( // It.IsAny<string>(), // It.IsAny<int>(), // It.IsAny<string>())) // .Returns(true); //Mocking methods with out parameter //bool isValidOutValue = true; //mockIdentityVerifier // .Setup(x => x.Validate( // "Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah", // out isValidOutValue)); //Mocking methods with ref parameter //mockIdentityVerifier // .Setup(x => x.Validate( // "Sarah", // 25, // "133 Pluralsight Drive, Draper, Utah", // ref It.Ref<IdentityVerificationStatus>.IsAny)) // .Callback(new ValidateCallback( // (string applicantName, // int applicantAge, // string applicantAddress, // ref IdentityVerificationStatus status) => status = new IdentityVerificationStatus(true))); //var mockScoreValue = new Mock<ScoreValue>(); //mockScoreValue.Setup(x => x.Score).Returns(300); //var mockScoreResult = new Mock<ScoreResult>(); //mockScoreResult.Setup(x => x.ScoreValue).Returns(mockScoreValue.Object); //var mockCreditScorer = new Mock<ICreditScorer>(); //mockCreditScorer.Setup(x => x.ScoreResult).Returns(mockScoreResult.Object); //Setup a mock property to return a specific value //mockCreditScorer.Setup(x => x.Score).Returns(300); //auto mocking property hierarchy (caveat: property must be marked as 'virtual' //var mockCreditScorer = new Mock<ICreditScorer>(); //mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //automatically populate properites (also nested objects) //var mockCreditScorer = new Mock<ICreditScorer>() //{ // DefaultValue = DefaultValue.Mock //}; var mockCreditScorer = new Mock <ICreditScorer>(); //Tracks changes of properties //mockCreditScorer.SetupProperty(x => x.Count); mockCreditScorer.SetupAllProperties(); //overrides any specific setup, should be used first mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(300); //system under test var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object); sut.Process(application); //veryfing that getter was called mockCreditScorer.VerifyGet(x => x.ScoreResult.ScoreValue.Score, Times.Once); //veryfing that setter was called (with value that was set) mockCreditScorer.VerifySet(x => x.Count = 1, Times.Once); Assert.That(application.GetIsAccepted(), Is.True); Assert.That(mockCreditScorer.Object.Count, Is.EqualTo(1)); }