-
Notifications
You must be signed in to change notification settings - Fork 0
Home
[this page is under development]
To when coding (tests) you might need different types randomised values.
VacheTacheLibrary provides this.
When doing automatic tests you need to populate your classes with data.
VacheTache is a helping library to do this quickly and in an ordered manner.
private void ActiveProjects_given_SeveralProjects_should_ReturnOnlyActiveProjects()
{
// Arrange.
var sut = One<Customer>()
.With(One<Project>(active:true)) // Strictly not necessary as active=true is standard behaviour.
.And
.With(One<Project>(active:false));
// Act.
var result = sut.ActiveProjects();
// Assert.
Assert.AreEqual(1, result);
Assert.IsTrue(result.Single().Active);
}
Behind the scenes the Customer gets a Name and Company and this Company gets a Name as all are mandatory. The Projects get unique names since the business rules are such and this is explicitly coded into the construct where it can be read, commented and version managed.
The naïve solution is to enter, by hand, some more or less random data into your objects. This has a number of drawbacks:
- Hand coding, even thought it is "asdf", "qwerty", 1234 and 111 is a waste of time.
- Your testing code gets filled with a lot of random data so it is hard to see what is really tested.
- "asdf" is really not appropriate data for, say,
customer.Name
.
Bad code:
private void ActiveProjects_given_SeveralProjects_should_ReturnOnlyActiveProjects()
{
// Arrange.
var sut = new Customer{
Name = "asdf",
Company = new Company{
Name = "asdf",
},
Projects = new []{
new Project{
Name = "asdf1",
Active = false
},
new Project{
Name = "asdf2",
Active = true
}
}
}
// Act.
var result = sut.ActiveProjects();
// Assert.
Assert.AreEqual(1, result);
Assert.IsTrue(result.Single().Active);
}
In the example above it is hard to figure out what in the sut that is involved in the test and what is needed just to setup the model. What is the Customer doing? Why is there a Company? Is the Name of the Projects important?