Question

I'm using an Object Mother for unit testing and I don't want to write all OM classes == duplicate class structure. Is it possible to create some base OM class which will generate OM class automatically?

class MyNewObjectMother: ObjectMother<SomeClass>{}

This should create all properties as public and method CreateInstance which create class object and inject all properties. Is it possible? What is the best practice? Or may be some auto T4 code generation, etc. thanks.

Was it helpful?

Solution

As shamp00 points out, a more maintainable alternative to Object Mother is the Test Data Builder pattern. Apart from NBuilder, you could also consider AutoFixture, which also implements the Test Data Builder pattern.

It'll enable you to create test instance like this:

var sc = fixture.Build<SomeClass>().With(x => x.Foo, "Foo").Build();

as well as many other features; the above example really only scratches the surface.

OTHER TIPS

You should check out NBuilder. It's a slightly different pattern than the ObjectMother (see this blog post).

You can do things like

Builder<Product>.CreateNew().With(x => x.Title = "some title").Build();
Builder<Product>.CreateListOfSize(10).WhereAll().Have(x => x.Title = "some title").Build();

You can also try T4 template for DTO: http://www.taimila.com/blog/dto-builders-with-t4/

It can be useful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top