[英]System.ArgumentNullException in unit testing
我只是一個初級開發人員,我還在研究我們公司的代碼庫。
這是我為hasProductImages
配置單元測試創建的代碼,但是我遇到了錯誤...
System.ArgumentNullException:值不能為null。
...在最后一個代碼中,可能是什么問題?
[TestClass]
public class ChooseProductViewModelTest
{
ChooseProductViewModel chooseProduct;
private EventAggregatorMock eventAggregator;
private ProductRepositoryMock productRepository;
private CategoryRepositoryMock categoryRepository;
private PosDeviceSettingsMock posDeviceSettings;
[TestInitialize]
public void Init()
{
eventAggregator = new EventAggregatorMock();
productRepository = new ProductRepositoryMock();
categoryRepository = new CategoryRepositoryMock();
posDeviceSettings = new PosDeviceSettingsMock();
chooseProduct = new ChooseProductViewModel(productRepository, categoryRepository, eventAggregator, posDeviceSettings);
}
[TestMethod]
public void ProductImageConfiguration_Verification_With_ProductItemViewModel()
{
chooseProduct = new ChooseProductViewModel(productRepository, categoryRepository, eventAggregator, posDeviceSettings);
Assert.IsTrue((chooseProduct.Items.First() as 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.