你如何在 abp 框架中使用 nSubstitute 模拟依赖关系,
How do you mock a dependency using nSubstitute in abp framework,
在 Abp 框架中,我正在尝试为我的 FamilyAppService class 编写一个测试(见下文)。我需要在 FamilyAppService 的构造函数中模拟一个 IAutho 实例。我尝试模拟 IAutho,然后将其添加到 FamilyAppService 的新实例(而不是使用 GetRequiredService()),但我收到 ObjectMapper 的 'System.ArgumentNullException' 错误。
家庭应用服务Class
public class FamilyAppService : KmAppService, IFamilyAppService {
private readonly IAutho autho;
public FamilyAppService(
IAutho autho) {
this.autho = autho;
}
public virtual async Task SendRequest(SendRequestInput input) {
var family = ObjectMapper.Map<FamilyDto, Family>(input.Family);
// code ...
await autho.FindUserIdByEmail(family.Email);
}
}
作者 Class。
我需要使用 nSubstitute
将 IAutho 依赖项替换为 Mocked class
public class Autho : IAutho, ITransientDependency {
public Autho( ) {
}
public virtual async Task<User> FindUserIdByEmail(string input) {
// Don’t want this code touched in test
// Code ...
}
}
我目前的测试...
[Fact]
public async Task ShouldSendEmail() {
var autho = Substitute.For<IAutho>();
autho.FindUserIdByEmail(Arg.Any<string>()).Returns((User)null);
var familyAppService = new FamilyAppService(autho);
// var familyAppService = GetRequiredService<IFamilyAppService>();
// setup input code here..
// get ObjectMapper error here
await familyAppService.SendRequest(input);
// Assert
}
github abp repo 的一位成员给了我正确的答案。您需要覆盖测试 class 上的 AfterAddApplication 方法并将 substitue/mock 添加到 services.AddSingletone.
示例...
protected override void AfterAddApplication(IServiceCollection services) {
var autho = Substitute.For<IAutho>();
autho.FindUserIdByEmail(Arg.Any<string>()).Returns((User)null);
services.AddSingleton(autho);
}
在 Abp 框架中,我正在尝试为我的 FamilyAppService class 编写一个测试(见下文)。我需要在 FamilyAppService 的构造函数中模拟一个 IAutho 实例。我尝试模拟 IAutho,然后将其添加到 FamilyAppService 的新实例(而不是使用 GetRequiredService()),但我收到 ObjectMapper 的 'System.ArgumentNullException' 错误。
家庭应用服务Class
public class FamilyAppService : KmAppService, IFamilyAppService {
private readonly IAutho autho;
public FamilyAppService(
IAutho autho) {
this.autho = autho;
}
public virtual async Task SendRequest(SendRequestInput input) {
var family = ObjectMapper.Map<FamilyDto, Family>(input.Family);
// code ...
await autho.FindUserIdByEmail(family.Email);
}
}
作者 Class。 我需要使用 nSubstitute
将 IAutho 依赖项替换为 Mocked classpublic class Autho : IAutho, ITransientDependency {
public Autho( ) {
}
public virtual async Task<User> FindUserIdByEmail(string input) {
// Don’t want this code touched in test
// Code ...
}
}
我目前的测试...
[Fact]
public async Task ShouldSendEmail() {
var autho = Substitute.For<IAutho>();
autho.FindUserIdByEmail(Arg.Any<string>()).Returns((User)null);
var familyAppService = new FamilyAppService(autho);
// var familyAppService = GetRequiredService<IFamilyAppService>();
// setup input code here..
// get ObjectMapper error here
await familyAppService.SendRequest(input);
// Assert
}
github abp repo 的一位成员给了我正确的答案。您需要覆盖测试 class 上的 AfterAddApplication 方法并将 substitue/mock 添加到 services.AddSingletone.
示例...
protected override void AfterAddApplication(IServiceCollection services) {
var autho = Substitute.For<IAutho>();
autho.FindUserIdByEmail(Arg.Any<string>()).Returns((User)null);
services.AddSingleton(autho);
}