簡體   English   中英

單元測試中的System.ArgumentNullException

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM