Skip to content

Commit 1736591

Browse files
committed
Added test to check what happens when your lambda isn't a property
1 parent 76f7369 commit 1736591

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NTestDataBuilder.Tests/Entities/Customer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public Customer(string firstName, string lastName, int yearJoined)
1616
YearJoined = yearJoined;
1717
}
1818

19+
public int CustomerForHowManyYears(DateTime since)
20+
{
21+
if (since.Year < YearJoined)
22+
throw new ArgumentException("Date must be on year or after year that customer joined.", "since");
23+
return since.Year - YearJoined;
24+
}
25+
1926
public string FirstName { get; private set; }
2027
public string LastName { get; private set; }
2128
public int YearJoined { get; private set; }

NTestDataBuilder.Tests/GetSetTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,14 @@ public void GivenNoValueHasBeenSetForAProperty_WhenRetrievingTheValueForThatProp
6666
Assert.That(ex.Message, Is.StringStarting("No value has been recorded yet for FirstName; consider using Has(x => x.FirstName) to check for a value first."));
6767
Assert.That(ex.ParamName, Is.EqualTo("property"));
6868
}
69+
70+
[Test]
71+
public void WhenRetrievingValueForANonProperty_ThenThrowAnException()
72+
{
73+
var ex = Assert.Throws<ArgumentException>(() => _b.Get(x => x.CustomerForHowManyYears(DateTime.Now)));
74+
75+
Assert.That(ex.Message, Is.StringStarting("Given property expression (x => x.CustomerForHowManyYears(DateTime.Now)) didn't specify a property on Customer"));
76+
Assert.That(ex.ParamName, Is.EqualTo("property"));
77+
}
6978
}
7079
}

NTestDataBuilder/DataBuilder.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ private static string GetPropertyName<TValue>(Expression<Func<TEntity, TValue>>
9494
{
9595
var memExp = property.Body as MemberExpression;
9696
if (memExp == null)
97-
throw new ArgumentException("Property must be valid on the entity object", "property");
97+
throw new ArgumentException(
98+
string.Format(
99+
"Given property expression ({0}) didn't specify a property on {1}",
100+
property,
101+
typeof(TEntity).Name
102+
),
103+
"property"
104+
);
98105

99106
return memExp.Member.Name;
100107
}

0 commit comments

Comments
 (0)