我目前正在为我已经添加到ASP.NET项目中的一些新功能制作一些UnitTests(不是它不是测试驱动设计)。我们使用NHibernate框架并使用UnitTest Mock-ing库FakeItEasy
。
我有以下课程&我想测试的方法:
public class Round
{
public static Round Create(List<Company> activeCompanies, Period period,
BusinessUser user, BusinessUser systemUser,
ISession session, IEntityQuery entityQuery,
RoundProcessBuilder processBuilder)
{
var round = new Round
{
Processes = new List<Process>();
Period = period,
CreationDate = DateTime.Now,
CreatedBy = user
};
// Save the Round in the DB so we can use it's Id in the Processes:
session.Save(round);
foreach (var company in activeCompanies)
{
var companyData = session.Get<CompanyData>(company.Id);
var processResult =
roundProcessBuilder.Build(
systemUser,
new CreateRoundProcessData(company, round, companyData),
entityQuery,
session);
processResult.HandleProcess(process =>
{
// serviceBus can stay null
process.Create(systemUser, DateTime.Now, session, null);
// No need to save the session here. If something went
// wrong we don't want halve of the processes being saved
round.Processes.Add(process);
// It's all or nothing
});
}
return round;
}
}
我主要想测试的是什么:当我使用这个Round#Create
方法时,让我们说100个活跃的公司,它应该创建100个进程,每个进程都应包含{{ 1}}。
到目前为止,这是我的UnitTest:
RoundId
我的问题在于以下代码:
[TestFixture]
public class RoundTest
{
private BusinessUser _systemUser;
private DateTime _creationDateRound1;
private List<Company> _activeCompanies;
private RoundProcessBuilder _roundProcessBuilder;
private ISession _session;
[SetUp]
public void Setup()
{
_creationDateRound1 = new DateTime(2015, 10, 5);
_systemUser = TestHelper.CreateBusinessUser(Role.Create("systemuser", "test",
Int32.MaxValue));
_activeCompanies = new List<Company>
{
TestHelper.CreateCompany();
};
_roundProcessBuilder = A.Fake<RoundProcessBuilder>();
_session = A.Fake<ISession>();
}
[Test]
public void TestCreateRoundWithoutPreviousRound()
{
var fakeExpectedRound = Round.Create(_activeCompanies, DateTime.Now.ToPeriod(),
_systemUser, _systemUser, _session, null, _roundProcessBuilder);
var fakeExpectedRoundData = RoundProcessData.Create(TestHelper.CreateCompany(),
fakeExpectedRound, new CompanyData());
var fakeExpectedProcess = new Process(_systemUser, null, "processName", null,
fakeExpectedRoundData, "controllerName", null);
var processSuccessResult = new ProcessSuccessResult(fakeExpectedProcess);
A.CallTo(() => _roundProcessBuilder.Build(null, null, null, null))
.WithAnyArguments()
.Returns(processSuccessResult);
A.CallTo(() => processSuccessResult.HandleProcess(A<Action<Process>>.Ignored))
.Invokes((Action<Process> action) => action(fakeExpectedProcess));
var round = Round.Create(_activeCompanies, _ceationDateRound1.ToPeriod(),
_systemUser, _systemUser, _session, null, _roundProcessBuilder);
Assert.AreEqual(_activeCompanies.Count, round.Processes.Count, "Number of processes");
Assert.AreEqual(round.Period.Quarter, Math.Ceiling(_creationDateRound1.Month / 3.0m), "Quarter");
Assert.AreEqual(round.Period.Year, round.Year, "Year");
// Test if each of the processes knows the RoundId, have the proper state,
// and are assigned to the systemuser
//foreach (var process in round.Processes)
//{
// var roundProcessData = process.ProcessData as RoundProcessData;
// Assert.IsNotNull(roundProcessData, "All processes should have RoundProcessData-objects as their data-object");
// Assert.AreEqual(roundProcessData.Round.Id, round.Id, "RoundId");
// Assert.AreEqual(process.Phase.State, PhaseState.Start, "Process state should be Start");
// Assert.AreEqual(process.AssignedTo, _systemUser, "AssignedTo should be systemuser");
//}
}
... // More tests
}
它会出现“A.CallTo(() => processSuccessResult.HandleProcess(A<Action<Process>>.Ignored))
.Invokes((Action<Process> action) => action(fakeExpectedProcess));
”错误。
我有这部分代码的原因是因为以下部分中的The specified object is not recognized as a fake object.
在没有它的情况下为null:
process
PS:我在我的UnitTest中使用额外的检查取消了对foreach的注释,因为当我模拟processResult.HandleProcess(process => // <- this was null
{
process.Create(systemUser, DateTime.Now, session, null);
round.Processes.Add(process);
});
本身时,它很可能是无用的。我的主要测试是如果创建了进程并将其添加到列表中给予活跃的公司。
答案 0 :(得分:2)
你的问题似乎是你试图将“假”逻辑添加到一个实际上不是假的对象:
// You create this as an instance of ProcessSuccessResult:
var processSuccessResult = new ProcessSuccessResult(fakeExpectedProcess);
...然后继续尝试在此处添加条件:
A.CallTo(() =>
processSuccessResult
.HandleProcess(A<Action<Process>>.Ignored))
.Invokes((Action<Process> action) => action(fakeExpectedProcess));
为了做到最后一点,变量processSuccessResult
将需要是一个虚假的接口实例,这样FakeItEasy可以使用它,并应用你想要的逻辑。
我假设ProcessSuccessResult
是您有权访问的课程,并且可以编辑?如果是这样,您应该能够添加一个接口,它将包含您需要的方法,以便您以后可以使用它。
一旦你定义了它,你应该能够按如下方式创建假对象,其中IProcessSuccessResult
将是你的界面的假实现,由FakeItEasy提供:
var processSuccessResult = A.Fake<IProcessSuccessResult>();
现在,您应该能够使用A.CallTo(...)
向该假对象添加逻辑。
当然,这意味着您的班级ProcessSuccessResult
的真实实现是不包含或通过变量processSuccessResult
调用。如果需要它的一部分,那么你可以尝试:
fakeProcessSuccessResult
和processSuccessResult
),并使用单独的测试来测试这个类的两个方面,并且它是用法。 我希望这很清楚,这对你有用。我知道有时可能会非常复杂,找到测试这样的事情的最佳策略。