public void NestedTrueFailTest() { // Arrange var expected = new TestClass { NestedTestClassProp = new TestClass { IntProp = 1 } }; var actual = new TestClass { NestedTestClassProp = new TestClass { IntProp = 2 } }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, true)); var expectedExceptionMessage = AggregateLines( " Values differ at property 'NestedTestClassProp.IntProp'.", " Expected: 1", " But was: 2", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void TestClassArrayWithNestedTrueFailsTest() { // Arrange var expected = new TestClass { TestClassArrayProp = new[] { new TestClass { IntProp = 1 }, new TestClass { StringProp = "A" } } }; var actual = new TestClass { TestClassArrayProp = new [] { new TestClass { IntProp = 1 }, new TestClass { StringProp = "B" } } }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, true)); var expectedExceptionMessage = AggregateLines( " Values differ at property 'TestClassArrayProp[1].StringProp'.", " String lengths are both 1. Strings differ at index 0.", " Expected: \"A\"", " But was: \"B\"", " -----------^", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void DoesNotTraverseSelfReferentialPropertiesTest() { // Arrange var expected = new TestClass(); expected.NestedTestClassProp = expected; expected.IntProp = 1; var actual = new TestClass(); actual.NestedTestClassProp = actual; actual.IntProp = 2; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, true)); // If the referential check was not present, the asserter would have navigated down to its maximum depth before eventually comparing the IntProp var expectedExceptionMessage = AggregateLines( " Values differ at property 'IntProp'.", " Expected: 1", " But was: 2", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void AssertFailsWithPropertyFilterTest() { // Arrange var filter = new PropertyFilter().AddFilter <TestClass>(t => new { t.StringProp }); var expected = new TestClass { IntProp = 1, StringProp = "A", }; var actual = new TestClass { IntProp = 2, StringProp = "B" }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, propertyFilter: filter)); var expectedExceptionMessage = AggregateLines( " Values differ at property 'StringProp'.", " String lengths are both 1. Strings differ at index 0.", " Expected: \"A\"", " But was: \"B\"", " -----------^", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
// [Test] public void Borrower_ShouldPersistAndRecall() { //---------------Set up test pack------------------- var quote = new QuoteBuilder() .WithRandomProps() .WithProp(b => b.Id = 0) .WithValidCompanyId() .Build(); //---------------Assert Precondition---------------- using (var ctx = new QBMSDbContext(_localDb.CreateConnection())) { //ctx.Set<Quote>().Add(quote); //ctx.Set(typeof (Quote)).Add(quote); //ctx.Set(quote.GetType()).Add(quote); //ctx.Entry(quote).State = EntityState.Added; ctx.Quotes.Add(quote); ctx.SaveChanges(); } using (var ctx = new QBMSDbContext(_localDb.CreateConnection())) { //---------------Execute Test ---------------------- var result = ctx.Set <Quote>().Single(); //---------------Test Result ----------------------- PropertyAssert.AreEqual(quote, result, "Company"); } }
public void NestedFalseTest() { // Arrange var expected = new TestClass { NestedTestClassProp = new TestClass { IntProp = 1 } }; var actual = new TestClass { NestedTestClassProp = new TestClass { IntProp = 1 } }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual)); // With nested=false, we think the objects differ, but we dont know how var expectedExceptionMessage = AggregateLines( " Values differ at property 'NestedTestClassProp'.", " Expected: <NUnit.Contrib.Tests.PropertyAssertTests+TestClass>", " But was: <NUnit.Contrib.Tests.PropertyAssertTests+TestClass>", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void NullNotEqualTest() { // Arrange TestClass expected = null; TestClass actual = new TestClass(); // Act, Assert Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual)); }
public void NullEqualTest() { // Arrange TestClass expected = null; TestClass actual = null; // Act, Assert // ReSharper disable ExpressionIsAlwaysNull PropertyAssert.AreEqual(expected, actual); // ReSharper restore ExpressionIsAlwaysNull }
public void AreEqual_GivenTwoDifferentTypedObjects_AndJustOnePropertyName_ComparesTheSameNamedPropertyOnBoth() { //---------------Set up test pack------------------- var obj1 = new { foo = "foo" }; var obj2 = new { bar = "bar", foo = "foo" }; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- Expect(() => PropertyAssert.AreEqual(obj1, obj2, "foo")) .Not.To.Throw(); //---------------Test Result ----------------------- }
public void AreEqual_ShouldBeAbleToFollowPropertyPaths() { //---------------Set up test pack------------------- var obj1 = new { foo = new { name = "bar" } }; var obj2 = new { foo = new { name = "bar" } }; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- Expect(() => PropertyAssert.AreEqual(obj1, obj2, "foo.name")) .Not.To.Throw(); //---------------Test Result ----------------------- }
public void CreateMessage_SetsMailMessagePropertyFromOwnProperty(string ownProp, string mmProp) { //---------------Set up test pack------------------- using (var email = CreateWithRandomRecipientAndSender()) { //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var msg = email.CreateMessage(); //---------------Test Result ----------------------- PropertyAssert.AreEqual(email, msg, ownProp, mmProp); } }
public void AreEqual_GivenTwoObjectsWithNonMatchingPropertyTypes_ShouldThrow() { //---------------Set up test pack------------------- var obj1 = new { foo = "1" }; var obj2 = new { bar = 1 }; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- Expect(() => PropertyAssert.AreEqual(obj1, obj2, "foo", "bar")) .To.Throw <AssertionException>(); //---------------Test Result ----------------------- }
public void CreateSMTPClient_SetsClientPropertyFromConfigProperty(string configProp, string clientProp) { //---------------Set up test pack------------------- var config = CreateRandomEmailConfig(); using (var email = CreateActualWithRandomRecipientAndSender(config)) { //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var client = email.CreateSMTPClient(); //---------------Test Result ----------------------- PropertyAssert.AreEqual(config, client, configProp, clientProp); } }
public void AreEqual_GivenTwoDifferentTypedObjects_AndPropertyNames_WhenPropertiesMatch_DoesNotThrow() { //---------------Set up test pack------------------- var obj1 = new { foo = "foo" }; var obj2 = new { bar = "foo" }; //---------------Assert Precondition---------------- Assert.AreNotEqual(obj1, obj2); Assert.AreNotEqual(obj1.GetType(), obj2.GetType()); //---------------Execute Test ---------------------- Expect(() => PropertyAssert.AreEqual(obj1, obj2, "foo", "bar")) .Not.To.Throw(); //---------------Test Result ----------------------- }
public void IntArrayDifferentLengthTest() { // Arrange var expected = new[] { 1, 2, 3 }; var actual = new[] { 1, 2 }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, true)); var expectedExceptionMessage = AggregateLines( "Values are not equal.", " Expected length: 3", " But was length: 2", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void IntArrayNotEqualTest() { // Arrange var expected = new[] { 1, 2 }; var actual = new[] { 1, 3 }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, true)); var expectedExceptionMessage = AggregateLines( " Values differ at index [1].", " Expected: 2", " But was: 3", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void IntNotEqualTest() { // Arrange const int expected = 1; const int actual = 2; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual)); var expectedExceptionMessage = AggregateLines( " Values are not equal.", " Expected: 1", " But was: 2", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void TestClassArrayWithNestedTruePassesTest() { // Arrange var expected = new TestClass { TestClassArrayProp = new [] { new TestClass { IntProp = 1 } } }; var actual = new TestClass { TestClassArrayProp = new [] { new TestClass { IntProp = 1 } } }; // Act, Assert Assert.DoesNotThrow(() => PropertyAssert.AreEqual(expected, actual, true)); }
public void AreEqual_WhenNullableAndNonNullableValues_AreEqual_ShouldNotThrow() { // Arrange var value = GetRandomDecimal(); var left = new HasADecimal(value); var right = new HasANullableDecimal(value); // Pre-Assert Expect(left.Value).To.Equal(right.Value); // Act Expect(() => { PropertyAssert.AreEqual(left, right, nameof(HasANullableDecimal.Value)); }) .Not.To.Throw(); // Assert }
public void AreEqual_WhenPropertyNotFoundByName_ShouldThrow() { //---------------Set up test pack------------------- var obj1 = new { foo = "foo" }; var obj2 = new { bar = "bar", foo = "foo" }; var badName = "foo1"; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- Expect(() => PropertyAssert.AreEqual(obj1, obj2, badName)) .To.Throw <AssertionException>() .With.Message.Containing("Unable to find property") .Then(badName); //---------------Test Result ----------------------- }
public void IntArrayPropDifferentLengthTest() { // Arrange var expected = new TestClass { IntArrayProp = new[] { 1, 2, 3, 4 } }; var actual = new TestClass { IntArrayProp = new[] { 1, 2, 4 } }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual, true)); var expectedExceptionMessage = AggregateLines( "Values differ at property 'IntArrayProp'.", " Expected length: 4", " But was length: 3", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void StringPropNotEqualTest() { // Arrange var expected = new TestClass { StringProp = "123" }; var actual = new TestClass { StringProp = null }; // Act, Assert var exception = Assert.Throws <AssertionException>(() => PropertyAssert.AreEqual(expected, actual)); var expectedExceptionMessage = AggregateLines( " Values differ at property 'StringProp'.", " Expected: \"123\"", " But was: null", ""); Assert.AreEqual(expectedExceptionMessage, exception.Message); }
public void AssertPassesWithPropertyFilterTest() { // Arrange var filter = new PropertyFilter().AddFilter <TestClass>(t => new { t.StringProp }); var expected = new TestClass { IntProp = 1, StringProp = "A", }; var actual = new TestClass { IntProp = 2, // Value is different, but the property is not in the filter StringProp = "A" }; // Act, Assert Assert.DoesNotThrow(() => PropertyAssert.AreEqual(expected, actual, propertyFilter: filter)); }
public void Spool_WhenOneEmailNotSent_CreatesEmailWithPropertiesFromDTO(string dtoProp, string emailProp) { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.BuildRandomWithRecipient(); var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.IsTrue(dto.EmailRecipients.Any()); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result PropertyAssert.AreEqual(dto, email, dtoProp, emailProp); }