2017-02-13 99 views
0

我想測試(使用using Microsoft.VisualStudio.TestTools.UnitTesting)此測試函數的頂部行導致引發DataMisalignedException將具有2個參數的函數轉換爲lambda動作

namespace xxx.Services.SupplierApiTests 
{ 
    [TestClass] 
    public class JsonSchemaValidatorTests 
    { 
     [TestMethod] 
     public void ShouldThrowOnBadPhoneNumber() 
     { 
      JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json"); 
      Action<IList, string> myAction = (x, y) => JsonSchemaValidator.validateAgainstJsonSchema(x, y); 
      Assert.ThrowsException<DataMisalignedException>(myAction); 
     } 
    } 
} 

如何使用JsonSchemaValidator.validateAgainstJsonSchema作爲一個行動,在兩個參數傳遞從測試的頂線?我的嘗試是在上面的代碼中,但沒有傳遞這兩個參數。

+0

我想你想'Assert.ThrowsException (()=> JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), 「./provider-schema.json」))',但我不能找到'Assert.ThrowsException'的文檔我通常只使用測試方法中的'ExpectedException'屬性。 – juharr

+0

@juharr是的謝謝,這就是我正在尋找。我發現它不是在轉換函數,而是在匿名函數中調用它。歡呼 – BeniaminoBaggins

+0

我的猜測是'Assert.ThrowsException'需要一個'Action'而不是'Action '。 – juharr

回答

1

爲了表明測試方法執行過程中預計會發生異常,您可以使用測試方法頂部的[ExpectedException]屬性。

[TestClass] 
public class JsonSchemaValidatorTests 
{ 
    [TestMethod] 
    [ExpectedException(typeof(DataMisalignedException))] 
    public void ShouldThrowOnBadPhoneNumber() 
    { 
     JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json"); 
    } 
} 
相關問題