public void ShouldBeEquivalentToReturnSuccess_FluentAssertFail() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "test description", ReferenceNumber = 12345 }; var expectedFooParam = new FooParam { Description = "tesdst description", ReferenceNumber = 123345 }; foo.CallBoo(fooParam); // When var testException = Assert.Throws <XunitException>(() => booMock.Verify(b => b.ItWorked(It.Is <BarParam>(o => o.ShouldBeEquivalentToReturnSuccess(expectedFooParam)))) ); // Then var expectedStartOfException = $"Expected member {nameof(expectedFooParam.Description)} to be {Environment.NewLine}" + $"\"{expectedFooParam.Description}\" with a length of {expectedFooParam.Description.Length}, but {Environment.NewLine}" + $"\"{fooParam.Description}\" has a length of {fooParam.Description.Length}.{Environment.NewLine}" + $"Expected member {nameof(expectedFooParam.ReferenceNumber)} to be {expectedFooParam.ReferenceNumber}, " + $"but found {fooParam.ReferenceNumber}."; Assert.StartsWith(expectedStartOfException, testException.Message); }
public void Its_EquivalentTo_WithDiffereningTypes_ThrowsAssertionExcpetion() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "actual description", ReferenceNumber = 12345 }; var expectedFooParam = new FooParam { Description = "expected description", ReferenceNumber = 54321 }; foo.CallBoo(fooParam); // When var testException = Assert.Throws <XunitException>(() => booMock.Verify(b => b.ItWorked(Its.EquivalentTo <BarParam, FooParam>(expectedFooParam))) ); // Then var expectedStartOfException = $"Expected member {nameof(expectedFooParam.Description)} to be {Environment.NewLine}\"{expectedFooParam.Description}\" with a length of {expectedFooParam.Description.Length}, " + $"but {Environment.NewLine}\"{fooParam.Description}\" has a length of {fooParam.Description.Length}." + $"{Environment.NewLine}Expected member {nameof(expectedFooParam.ReferenceNumber)} to be {expectedFooParam.ReferenceNumber}, " + $"but found {fooParam.ReferenceNumber}."; Assert.StartsWith(expectedStartOfException, testException.Message); }
public void ShouldBeEquivalentToReturnSuccess() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "test description", ReferenceNumber = 12345 }; // When foo.CallBoo(fooParam); // Then booMock.Verify(b => b.ItWorked(It.Is <BarParam>(o => o.ShouldBeEquivalentToReturnSuccess(fooParam)))); }
public void It_IsEquivilentToMatcher_VerifySuccess_WithDiffereningTypes() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "test description", ReferenceNumber = 12345 }; var expectedFooParam = new FooParam { Description = fooParam.Description + "", ReferenceNumber = fooParam.ReferenceNumber }; foo.CallBoo(fooParam); // When booMock.Verify(b => b.ItWorked(It <BarParam> .IsEquivalentTo(expectedFooParam))); // Then // No Exception thrown }
public void Its_EquivalentTo_DoesNotThrowException() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "test description", ReferenceNumber = 12345 }; var expectedBarParam = new BarParam { Description = fooParam.Description, ReferenceNumber = fooParam.ReferenceNumber }; foo.CallBoo(fooParam); // When booMock.Verify(b => b.ItWorked(Its.EquivalentTo(expectedBarParam))); // Then // No Exception thrown }
public void It_IsEquivilentToMatcher_VerifyFail_WithDiffereningTypes() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "test description", ReferenceNumber = 12345 }; var expectedFooParam = new FooParam { Description = "something else", ReferenceNumber = 54321 }; foo.CallBoo(fooParam); // When, Then var testException = Assert.Throws <MockException>(() => booMock.Verify(b => b.ItWorked(It <BarParam> .IsEquivalentTo(expectedFooParam)), Times.Once) ); Assert.StartsWith("Expected invocation on the mock once, but was 0 times", testException.Message.Trim()); }
public void Its_EquivalentTo_ThrowsException_WithMultipleSetups() { // Given var booMock = new Mock <IBar>(); var foo = new Foo(booMock.Object); var fooParam = new FooParam { Description = "given value 1", ReferenceNumber = 12345 }; var fooParam2 = new FooParam { Description = "given value 2", ReferenceNumber = 23456 }; var expectedBarParam = new BarParam { Description = fooParam.Description, ReferenceNumber = fooParam.ReferenceNumber }; var expectedBarParam2 = new BarParam { Description = fooParam2.Description, ReferenceNumber = fooParam2.ReferenceNumber }; var unExpectedBarParam = new BarParam { Description = "Unexpected value" }; booMock.Setup(b => b.ItWorked(Its.EquivalentTo(expectedBarParam, true))); booMock.Setup(b => b.ItWorked(Its.EquivalentTo(expectedBarParam2, true))); foo.CallBoo(fooParam); foo.CallBoo(fooParam2); // When booMock.Verify(b => b.ItWorked(Its.EquivalentTo(expectedBarParam, true)), Times.Once); booMock.Verify(b => b.ItWorked(Its.EquivalentTo(expectedBarParam2, true)), Times.Once); // and When Then var testException = Assert.Throws <MockException>(() => booMock.Verify(b => b.ItWorked(Its.EquivalentTo(unExpectedBarParam, true)), Times.Once) ); Assert.StartsWith("Expected invocation on the mock once, but was 0 times", testException.Message.Trim()); }
public void CallBoo(FooParam param) { _bar.ItWorked(new BarParam { Description = param.Description, ReferenceNumber = param.ReferenceNumber }); }