模拟方法因 NULL 而失败
Mock Method Failing due to NULL
我遇到一个问题,我的被测系统上的一个方法由于 NULL 引用异常而失败。我可以看到问题出在哪里,但我不确定如何解决它。
我正在测试的class,简单来说如下:
public class PaymentService
{
public PaymentService(ICompanyService companyService, //Other dependencies)
{
_companyService = companyService;
//Other dependencies being assigned
}
public string SetupPayment(User user, ProductVariant product,
string successUrl, string failureUrl,
string cancelUrl, string errorUrl)
{
var company = _companyService.GetCompanyById(user.GetAppointment().RegistrationNumber);
}
}
PaymentService
class还有很多其他的交互,但是对于这个问题来说,这才是最重要的。
如您所见,要在CompanyService
中调用GetCompanyId
方法,需要满足user.GetAppointment().RegistrationNumber
传入的参数。 User.GetAppointment
的实现如下:
public Appointment GetAppointment()
{
// Check if user has any appointments
if (Appointments == null || Appointments.Count == 0) return null;
return Appointments.First().Appointment;
}
在我的单元测试中,我现在正在尝试测试 SetupPayment
方法,但每次运行时,它都会在运行 user.GetAppointment
方法时失败,因为它总是 returns null
。
我在单元测试中的模拟框架已经这样设置了(提请注意_companyService
的模拟:
var companyServiceMock = new Mock<ICompanyService>();
companyServiceMock.Setup(x => x.GetCompanyByRegistrationNumber(It.IsAny<string>()))
.Returns(new Company()
{
RegistrationNumber = "12333",
});
_paymentService = new PaymentService(_companyService, _accountService, _settings, _settingsHelper, _scheduleManagerService, _appSettingService);
问题是你的User
没有Appointments
,所以GetAppointment
returnnull
。当您创建 User
时,您可以设置 Appointments
吗?如果没有,我建议添加一个接口 - IUser
让我们说 - 到 User
class 并将其传递进去,这样你就可以传递 IUser
的模拟并模拟方法 GetAppointment
.
我遇到一个问题,我的被测系统上的一个方法由于 NULL 引用异常而失败。我可以看到问题出在哪里,但我不确定如何解决它。
我正在测试的class,简单来说如下:
public class PaymentService
{
public PaymentService(ICompanyService companyService, //Other dependencies)
{
_companyService = companyService;
//Other dependencies being assigned
}
public string SetupPayment(User user, ProductVariant product,
string successUrl, string failureUrl,
string cancelUrl, string errorUrl)
{
var company = _companyService.GetCompanyById(user.GetAppointment().RegistrationNumber);
}
}
PaymentService
class还有很多其他的交互,但是对于这个问题来说,这才是最重要的。
如您所见,要在CompanyService
中调用GetCompanyId
方法,需要满足user.GetAppointment().RegistrationNumber
传入的参数。 User.GetAppointment
的实现如下:
public Appointment GetAppointment()
{
// Check if user has any appointments
if (Appointments == null || Appointments.Count == 0) return null;
return Appointments.First().Appointment;
}
在我的单元测试中,我现在正在尝试测试 SetupPayment
方法,但每次运行时,它都会在运行 user.GetAppointment
方法时失败,因为它总是 returns null
。
我在单元测试中的模拟框架已经这样设置了(提请注意_companyService
的模拟:
var companyServiceMock = new Mock<ICompanyService>();
companyServiceMock.Setup(x => x.GetCompanyByRegistrationNumber(It.IsAny<string>()))
.Returns(new Company()
{
RegistrationNumber = "12333",
});
_paymentService = new PaymentService(_companyService, _accountService, _settings, _settingsHelper, _scheduleManagerService, _appSettingService);
问题是你的User
没有Appointments
,所以GetAppointment
returnnull
。当您创建 User
时,您可以设置 Appointments
吗?如果没有,我建议添加一个接口 - IUser
让我们说 - 到 User
class 并将其传递进去,这样你就可以传递 IUser
的模拟并模拟方法 GetAppointment
.