forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicSetterExceptionFixture.cs
59 lines (53 loc) · 1.53 KB
/
BasicSetterExceptionFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using NHibernate.Properties;
using NUnit.Framework;
namespace NHibernate.Test.PropertyTest
{
/// <summary>
/// Test the exception messages that come out a BasicSetter when
/// invalid values are passed in.
/// </summary>
[TestFixture]
public class BasicSetterExceptionFixture
{
protected IPropertyAccessor _accessor;
protected ISetter _setter;
[SetUp]
public void SetUp()
{
_accessor = PropertyAccessorFactory.GetPropertyAccessor("property");
_setter = _accessor.GetSetter(typeof(A), "Id");
}
[Test]
public void SetInvalidType()
{
A instance = new A();
var e = Assert.Throws<PropertyAccessException>(() => _setter.Set(instance, "wrong type"));
Assert.That(e.Message, Is.EqualTo("The type System.String can not be assigned to a property of type System.Int32 setter of NHibernate.Test.PropertyTest.BasicSetterExceptionFixture+A.Id"));
}
[Test]
public void SetValueArgumentException()
{
A instance = new A();
// this will throw a TargetInvocationException that gets wrapped in a PropertyAccessException
var e = Assert.Throws<PropertyAccessException>(() => _setter.Set(instance, 5));
Assert.That(e.Message, Is.EqualTo("could not set a property value by reflection setter of NHibernate.Test.PropertyTest.BasicSetterExceptionFixture+A.Id"));
}
public class A
{
private int _id = 0;
public int Id
{
get { return _id; }
set
{
if (value == 5)
{
throw new ArgumentException("can't be 5 for testing purposes");
}
_id = value;
}
}
}
}
}