C# 单元测试中的系统异常

C# 单元测试中的系统异常,c#,unit-testing,C#,Unit Testing,我只是一名初级开发人员,还在我们公司学习代码库。 下面是我为单元测试hasProductImages配置创建的代码,但是我有一个错误 System.ArgumentNullException:值不能为null …在上一个代码中,可能有什么问题 [TestClass] 公共类ChooseProductViewModelTest { 选择产品视图模型选择产品; 私有事件聚合器模拟事件聚合器; 私有ProductRepositoryMock productRepository; 私有分类存储模拟分类存

我只是一名初级开发人员,还在我们公司学习代码库。
下面是我为单元测试hasProductImages配置创建的代码,但是我有一个错误

System.ArgumentNullException:值不能为null

…在上一个代码中,可能有什么问题

[TestClass]
公共类ChooseProductViewModelTest
{
选择产品视图模型选择产品;
私有事件聚合器模拟事件聚合器;
私有ProductRepositoryMock productRepository;
私有分类存储模拟分类存储;
私有posDeviceSettings模拟posDeviceSettings;
[测试初始化]
公共void Init()
{
eventAggregator=新的EventAggregatorMock();
productRepository=new ProductRepositoryMock();
categoryRepository=新CategoryRepositoryMock();
posDeviceSettings=新的PosDeviceSettingsMock();
chooseProduct=new ChooseProductViewModel(productRepository、categoryRepository、eventAggregator、posDeviceSettings);
}
[测试方法]
public void ProductImageConfiguration_验证_与_ProductItemViewModel()
{
chooseProduct=new ChooseProductViewModel(productRepository、categoryRepository、eventAggregator、posDeviceSettings);
Assert.IsTrue((选择product.Items.First()作为ProductItemViewModel.hasProductImages);
}
}

试试这个,开始调查它返回null的原因

[TestMethod]
public void ProductImageConfiguration_Verification_With_ProductItemViewModel()
{
    chooseProduct = new ChooseProductViewModel(productRepository, categoryRepository, eventAggregator, posDeviceSettings);
    var productItemViewModel = chooseProduct.Items.First() as ProductItemViewModel; //This line will obviously produce null
    Assert.IsNotNull(productItemViewModel);
    Assert.IsTrue(productItemViewModel.hasProductImages);
}

试试这个,开始调查它返回null的原因

[TestMethod]
public void ProductImageConfiguration_Verification_With_ProductItemViewModel()
{
    chooseProduct = new ChooseProductViewModel(productRepository, categoryRepository, eventAggregator, posDeviceSettings);
    var productItemViewModel = chooseProduct.Items.First() as ProductItemViewModel; //This line will obviously produce null
    Assert.IsNotNull(productItemViewModel);
    Assert.IsTrue(productItemViewModel.hasProductImages);
}

谢谢,但它并没有解决我的问题,也许我需要进一步研究它返回null的原因,当然,因为您没有为ChooseProductViewModel提供代码,所以我们所能做的就是猜测。尝试查看Items枚举中的第一个项是否为ProfactItemViewModel类型,或者重写为
choseProduct.Items.OfType().first()
不要发表“Thank you”注释,请阅读Thank,但它并没有解决我的问题,也许我需要进一步研究它返回null Thank的原因,当然,因为您没有为ChooseProductViewModel提供代码,所以我们只能猜测。尝试查看Items枚举中的第一项是否为ProfImpectItemViewModel类型,或者重写为
choseProduct.Items.OfType().first()
不要发表“谢谢”评论,请阅读