Ron*_*ijm 6 c# unit-testing moq autofixture
我有一个非常大的应用程序,它使用了许多其他服务.对于测试场景,我不希望我的单元测试依赖于第三方系统,因此我想用假货或模拟或其他任何方式替换服务.
我已经完成了大部分的艰苦劳动,用一个替换了具体的服务IService
.具体的服务与DI框架连接
现在我想用随机生成的假货替换那些.
public interface ISimpleService
{
int Fizz(int input);
string Buzz(string input);
}
Run Code Online (Sandbox Code Playgroud)
public interface ISimpleServiceFactory
{
ISimpleService Create();
}
Run Code Online (Sandbox Code Playgroud)
public static void Main(string[] args)
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var service = fixture.Create<ISimpleService>();
var fizzResult = service.Fizz(42);
var buzzResult = service.Buzz("I'd like something random...");
}
Run Code Online (Sandbox Code Playgroud)
这显示了我基本上想要的东西.我只想让autofixture为我创建一些动态代理对象,方法返回接口中指定类型的随机类...
请注意,我在这里使用过AutoMoq,因为默认情况下Fixture不想从接口创建对象,但我也尝试过其他框架:FakeItEasy,AutoRhinoMock
public static void Main(string[] args)
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var serviceMock = fixture.Create<Mock<ISimpleService>>();
// These two lines below cause the magic now
serviceMock.Setup(x => x.Fizz(It.IsAny<int>())).Returns(fixture.Create<int>());
serviceMock.Setup(x => x.Buzz(It.IsAny<string>())).Returns(fixture.Create<string>());
var service = serviceMock.Object;
var fizzResult = service.Fizz(42);
var buzzResult = service.Buzz("I'd like something random...");
}
Run Code Online (Sandbox Code Playgroud)
这确实给了我想要的结果:带有随机int的fizzResult,带有随机字符串的buzzResult(默认情况下为guid)但是,这只是一个小例子,我的实际服务引用要大得多,最多100个方法. ..(他们是外部肥皂服务等,无法帮助它)我不想手动设置所有东西,如果这是一个通用的解决方案,那将是伟大的...
所以,你可能已经注意到了,我也发布了一个ISimpleServiceFactory
界面.这类似于实际情况,因为实际的具体ISimpleService
需要一堆配置.所以,如果我们使用那种工作解决方案,我们就会这样:
public static void Main(string[] args)
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var serviceFactoryMock = fixture.Create<Mock<ISimpleServiceFactory>>();
var serviceMockDelegate = new Func<ISimpleService>(() =>
{
var serviceMock = fixture.Create<Mock<ISimpleService>>();
serviceMock.Setup(x => x.Fizz(It.IsAny<int>())).Returns(fixture.Create<int>());
serviceMock.Setup(x => x.Buzz(It.IsAny<string>())).Returns(fixture.Create<string>());
return serviceMock.Object;
});
serviceFactoryMock.Setup(x => x.Create()).Returns(serviceMockDelegate);
var service = serviceFactoryMock.Object.Create();
var fizzResult = service.Fizz(42);
var buzzResult = service.Buzz("I'd like something random...");
}
Run Code Online (Sandbox Code Playgroud)
这似乎有点乱,这是一个非常小的界面.实际的服务有很多层次,有100多种方法.
对于我的代码中需要特定条件的方法,我显然仍会手动设置这些条件,但默认情况下其他所有内容都应该是随机值.生成大量随机对象也允许进行一些模糊测试
有没有办法在没有所有这些手册设置的情况下自动生成随机对象?
您不需要工厂,也不需要在接口中设置每个方法,如果我理解正确的话,您只是想使用Fixture创建一个代理,该代理为您在该代理上调用的每个方法返回随机值。使用AutoConfiguredMoqCustomization而不是使用AutoMoqCustomization。它全部位于名为 Fixture.AutoMoq 的 nuget 包中。
class Program
{
static void Main(string[] args)
{
}
}
[TestFixture]
public class program
{
[Test]
public void some_test()
{
var fixture = new Fixture();
fixture.Customize(new AutoConfiguredMoqCustomization());
var simpleServices = fixture.CreateMany<ISimpleService>();
foreach (var simpleService in simpleServices)
{
string randomString = simpleService.Buzz("hello");
int randomInt = simpleService.Fizz(15);
}
}
}
public interface ISimpleService
{
int Fizz(int input);
string Buzz(string input);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3007 次 |
最近记录: |