我想编写一个检查给定命名空间内的类的测试。如果它们返回null,则必须检查返回任何类型列表的类的所有方法。如果是这样,测试必须失败。
类/方法本身也有依赖项(构造函数参数和方法参数),应该自动插入。
AutoFixture是否有机制来检查返回任何类型列表的方法永远不会返回null?
示例类:
public class UserService
{
private readonly IRemotingFacade _remotingFacade;
public UserService(IRemotingFacade remotingFacade)
{
_remotingFacade = remotingFacade;
}
// directly return a list
public IEnumerable<User> GetUsers()
{
}
// directly return a list, pass method parameters
public IEnumerable<User> GetUsers(string filter)
{
}
// wrapped list
public IBusinessResponse<IEnumerable<User>> GetUsers()
{
}
// wrapped list, pass method parameters
public IBusinessResponse<IEnumerable<User>> GetUsers(string filter)
{
}
}
所以请说明列表可以包含在另一个对象中。
答案 0 :(得分:5)
然而,我认为这是一个很好的主意(我不知道为什么我之前没有想到这一点),所以我现在添加了a new task to the backlog
答案 1 :(得分:5)
AutoFixture 3.18.0引入了一个名为Idioms.FsCheck的新胶水库,它使用FsCheck来实现可重用的
断言名为ReturnValueMustNotBeNullAssertion
。
这个新断言验证(或至少可能使得返回值(Query)的方法不返回null。
<强>安装强>
Idioms.FsCheck可在NuGet上找到:
PM> Install-Package AutoFixture.Idioms.FsCheck
<强>方案强>
UserService
使用注入的IRemotingFacade
实例并公开两个 [1] 查询:
场景#1 :注入的IRemotingFacade
实例返回null:
[Theory, UnitTestConventions]
public void Scenario1(
ISpecimenBuilder builder,
[Frozen]Mock<IRemotingFacade> stub)
{
stub.Setup(x => x.GetUsers()).Returns((User[])null);
var sut = from x in new Methods<UserService>() select x.GetUsers();
var assertion = new ReturnValueMustNotBeNullAssertion(builder);
Assert.Throws<ReturnValueMustNotBeNullException>(() =>
assertion.Verify(sut));
}
场景#2 :注入的IRemotingFacade
实例不会返回null:
[Theory, UnitTestConventions]
public void Scenario2(
ISpecimenBuilder builder,
[Frozen]Mock<IRemotingFacade> stub,
User[] users)
{
stub.Setup(x => x.GetUsers()).Returns(users);
var sut = from x in new Methods<UserService>() select x.GetUsers();
var assertion = new ReturnValueMustNotBeNullAssertion(builder);
Assert.DoesNotThrow(() => assertion.Verify(sut));
}
场景3 :如果i
-1 GetUsers(int)
,则返回null:
[Theory, UnitTestConventions]
public void Scenario3(
ISpecimenBuilder builder,
[Frozen]Mock<IRemotingFacade> stub,
User[] users)
{
stub.Setup(x => x.GetUsers()).Returns(users);
var sut = from x in new Methods<UserService>()
select x.GetUsers(default(int));
var assertion = new ReturnValueMustNotBeNullAssertion(builder);
Assert.Throws<ReturnValueMustNotBeNullException>(
() => assertion.Verify(sut));
}
<强>说明强>
If you only have F# 3.1 installed,您还可以在 app.config 文件中添加程序集绑定重定向:
<dependentAssembly>
<assemblyIdentity name="FSharp.Core"
publicKeyToken="b03f5f7f11d50a3a"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.3.1.0"
newVersion="4.3.1.0" />
</dependentAssembly>
UnitTestConventionsAttribute
定义为:
internal class UnitTestConventionsAttribute : AutoDataAttribute
{
internal UnitTestConventionsAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
使用Albedo执行反射查询。
[1] 对于演示,我简化了original code,如下所示:
public class User
{
}
public interface IRemotingFacade
{
User[] GetUsers();
}
public class UserService
{
private readonly IRemotingFacade remotingFacade;
public UserService(IRemotingFacade remotingFacade)
{
if (remotingFacade == null)
throw new ArgumentNullException("remotingFacade");
this.remotingFacade = remotingFacade;
}
public User[] GetUsers()
{
return this.remotingFacade.GetUsers();
}
public User[] GetUsers(int i)
{
if (i == -1)
return null;
return this.remotingFacade.GetUsers();
}
}