-
Notifications
You must be signed in to change notification settings - Fork 0
6. Unit tests
Dmitry Savchenko edited this page Jul 15, 2016
·
3 revisions
Let’s cover the written code with tests. The first one is responsible for testing of Human entity mapping. Add the file When_save_Human.cs to the folder Persisteces of the UnitTests project.
``` namespace Example.UnitTests { #region << Using >>using Example.Domain;
using Incoding.MSpecContrib;
using Machine.Specifications;
#endregion
[Subject(typeof(Human))]
public class When_save_Human : SpecWithPersistenceSpecification<Human>
{
#region Fields
It should_be_verify = () => persistenceSpecification.VerifyMappingAndSchema();
#endregion
}
}
<p>The test works with a test database (Example_test): an instance of the Human class with automatically populated fields is created, then stored in the DB, retrieved from and compared to the created instance.</p>
<p>Then add the tests for WhereSpecifications in a folder named Specifications.</p>
<h6><strong>When_human_by_first_name.cs</strong></h6>
namespace Example.UnitTests { #region << Using >>
using System;
using System.Collections.Generic;
using System.Linq;
using Example.Domain;
using Incoding.MSpecContrib;
using Machine.Specifications;
#endregion
[Subject(typeof(Human.Where.ByFirstName))]
public class When_human_by_first_name
{
#region Fields
Establish establish = () =>
{
Func<string, Human> createEntity = (firstName) =>
Pleasure.MockStrictAsObject<Human>(mock =>
mock.SetupGet(r => r.FirstName)
.Returns(firstName));
fakeCollection = Pleasure.ToQueryable(createEntity(Pleasure.Generator.TheSameString()),
createEntity(Pleasure.Generator.String()));
};
Because of = () =>
{
filterCollection = fakeCollection
.Where(new Human.Where.ByFirstName(Pleasure.Generator.TheSameString()).IsSatisfiedBy())
.ToList();
};
It should_be_filter = () =>
{
filterCollection.Count.ShouldEqual(1);
filterCollection[0].FirstName.ShouldBeTheSameString();
};
#endregion
#region Establish value
static IQueryable<Human> fakeCollection;
static List<Human> filterCollection;
#endregion
}
}
<h6><strong>When_human_by_last_name.cs</strong></h6>
namespace Example.UnitTests { #region << Using >>
using System;
using System.Collections.Generic;
using System.Linq;
using Example.Domain;
using Incoding.MSpecContrib;
using Machine.Specifications;
#endregion
[Subject(typeof(Human.Where.ByLastName))]
public class When_human_by_last_name
{
#region Fields
Establish establish = () =>
{
Func<string, Human> createEntity = (lastName) =>
Pleasure.MockStrictAsObject<Human>(mock =>
mock.SetupGet(r => r.LastName)
.Returns(lastName));
fakeCollection = Pleasure.ToQueryable(createEntity(Pleasure.Generator.TheSameString()),
createEntity(Pleasure.Generator.String()));
};
Because of = () =>
{
filterCollection = fakeCollection
.Where(new Human.Where.ByLastName(Pleasure.Generator.TheSameString()).IsSatisfiedBy())
.ToList();
};
It should_be_filter = () =>
{
filterCollection.Count.ShouldEqual(1);
filterCollection[0].LastName.ShouldBeTheSameString();
};
#endregion
#region Establish value
static IQueryable<Human> fakeCollection;
static List<Human> filterCollection;
#endregion
}
}
<p>Now we have to add tests for the command and the query (Operations folder). For the command, you need to add two tests: the first one verifies the creation of a new entity; the second one verifies the editing of an existing entity.</p>
<h6><strong>When_get_people_query.cs</strong></h6>
namespace Example.UnitTests { #region << Using >>
using System.Collections.Generic;
using Example.Domain;
using Incoding.Extensions;
using Incoding.MSpecContrib;
using Machine.Specifications;
#endregion
[Subject(typeof(GetPeopleQuery))]
public class When_get_people
{
#region Fields
Establish establish = () =>
{
var query = Pleasure.Generator.Invent<GetPeopleQuery>();
human = Pleasure.Generator.Invent<Human>();
mockQuery = MockQuery<GetPeopleQuery, List<GetPeopleQuery.Response>>
.When(query)
.StubQuery(whereSpecification: new Human.Where.ByFirstName(query.Keyword)
.Or(new Human.Where.ByLastName(query.Keyword)),
entities: human);
};
Because of = () => mockQuery.Original.Execute();
It should_be_result = () => mockQuery.ShouldBeIsResult(list => list.ShouldEqualWeakEach(new List<Human>() { human },
(dsl, i) => dsl.ForwardToValue(r => r.Birthday, human.Birthday.ToShortDateString())
.ForwardToValue(r => r.Sex, human.Sex.ToString())
));
#endregion
#region Establish value
static MockMessage<GetPeopleQuery, List<GetPeopleQuery.Response>> mockQuery;
static Human human;
#endregion
}
}
<h6><strong>When_add_human.cs</strong></h6>
namespace Example.UnitTests { #region << Using >>
using Example.Domain;
using Incoding.MSpecContrib;
using Machine.Specifications;
#endregion
[Subject(typeof(AddOrEditHumanCommand))]
public class When_add_human
{
#region Fields
Establish establish = () =>
{
var command = Pleasure.Generator.Invent<AddOrEditHumanCommand>();
mockCommand = MockCommand<AddOrEditHumanCommand>
.When(command)
.StubGetById<Human>(command.Id, null);
};
Because of = () => mockCommand.Original.Execute();
It should_be_saved = () => mockCommand.ShouldBeSaveOrUpdate<Human>(human => human.ShouldEqualWeak(mockCommand.Original));
#endregion
#region Establish value
static MockMessage<AddOrEditHumanCommand, object> mockCommand;
#endregion
}
}
<h6><strong>When_edit_human.cs</strong></h6>
namespace Example.UnitTests { #region << Using >>
using Example.Domain;
using Incoding.MSpecContrib;
using Machine.Specifications;
#endregion
[Subject(typeof(AddOrEditHumanCommand))]
public class When_edit_human
{
#region Fields
Establish establish = () =>
{
var command = Pleasure.Generator.Invent<AddOrEditHumanCommand>();
human = Pleasure.Generator.Invent<Human>();
mockCommand = MockCommand<AddOrEditHumanCommand>
.When(command)
.StubGetById(command.Id, human);
};
Because of = () => mockCommand.Original.Execute();
It should_be_saved = () => mockCommand.ShouldBeSaveOrUpdate<Human>(human => human.ShouldEqualWeak(mockCommand.Original));
#endregion
#region Establish value
static MockMessage<AddOrEditHumanCommand, object> mockCommand;
static Human human;
#endregion
}
}
<br/>
<ul>
<li><a href="/IncodingSoftware/get-started/wiki/5.-Specifications:-data-filters">Previous</a></li>
</ul>