Automated testing of getters / seters properties

We use support fields for many properties on our domain objects, for example:

protected string _firstname; public virtual string Firstname { get { return _firstname; } set { _firstname = value; } } 

I sometimes made silly typos, as in the example below, and would like to write one test that checks all these properties, and not manually perform a test on a single object.

 public virtual string Firstname { get { return _firstname; } set { _firstname = Firstname; } } 

Would it be easy to write, or does a library already exist to validate these support fields? This will only be done for properties with setters and (presumably) for a support field that matches the property name using the camel-case underscore

+4
source share
3 answers

Another solution would be to use automatic properties to fix this problem:

 public virtual string FirstName { get; set; } 

UPDATE (see comments, a support field seems necessary): Another possibility is to create pocos. Simple t4 template 'Person.tt'

 <#@ template language="C#" #> <# var pocos = new [] { Tuple.Create("FirstName", "string"), Tuple.Create("LastName", "string"), Tuple.Create("Age", "int")}; #> public partial class Person { <# foreach(var t in pocos) {#> protected <#= t.Item2#> _<#= t.Item1.ToLowerInvariant()#>; public virtual <#= t.Item2#> <#= t.Item1#> { get { return _<#= t.Item1.ToLowerInvariant()#>; } set { _<#= t.Item1.ToLowerInvariant()#> = value; } } <#}#> } 

Now this, of course, can bring as many problems with it as it solves, but maybe it's worth a look ... maybe :)

+5
source

In addition to using the properties of a car, I would think about using reflection to test my models ..

Just write a generic method that gets all the properties of your class, and then use these methods:

 / get value of property: public double Number double value = (double)numberPropertyInfo.GetValue(calcInstance, null); [C#] // set value of property: public double Number numberPropertyInfo.SetValue(calcInstance, 10.0, null); 

In your example:

 void Main() { const int testValue=5; var test = (Test)Activator.CreateInstance(typeof(Test)); PropertyInfo valuePropertyInfo = typeof(Test).GetProperty("Value"); valuePropertyInfo.SetValue(test, testValue, null); int value = (int)valuePropertyInfo.GetValue(test, null); Console.Write(value); //Assert here instead } public class Test { private int _value; public int Value {get {return _value;} set{_value=Value;}} } 

the output of the above function is 0 instead of the expected 5. claiming that this could cause an error.

What do you think of this approach.

+2
source

Gallio / MbUnit has a verifier that does exactly what you are looking for. A typical use of AccessContract is the following:

 public class Foo // Dummy reference type. { private readonly int value; public int Value { get { return value; } } public Foo (int value) { this.value = value; } } public class Bar { private Foo foo; public Bar(string unusedParameter) { } public Foo Foo // A complex property to be tested! { get { return foo; } set { if (value == null) throw new ArgumentNullException("value"); if (value.Value < 0) throw new ArgumentOutOfRangeException(); if (value.Value == 666) throw new ArgumentException("Inferno value strictly forbidden."); foo = value; } } } 

And a test device that uses AccessorContract to run various tests in the property.

 [TestFixture] public class BarTest { [VerifyContract] public readonly IContract AccessorTests = new AccessorContract<Bar, Foo> { Getter = target => target.Foo, Setter = (target, value) => target.Foo = value, ValidValues = { new Foo(123), new Foo(456), new Foo(789) }, AcceptNullValue = false, DefaultInstance = () => new Bar("Hello"), InvalidValues = { { typeof(ArgumentOutOfRangeException), new Foo(-123), new Foo(-456) }, { typeof(ArgumentException), new Foo(666) } } }; } 

The contract controller generates the following unit tests:

enter image description here

See the MbUnit test project for more usage examples.

+1
source

All Articles