2017-09-14 19 views
1
public async Task<HttpResponseMessage> UpdateUserProfile(HttpPostedFile postedFile) 
{ 
    //update operations 
} 

HttpPostedFileを使用している人の画像を更新するUpdateUserProfileメソッドがあります。それはPostman/Swaggerからうまくいきます。今、同じもののUnitTestCasesを書いています。私は私がやろうとしたができなかったのです手動でコードからHttpPostedFileオブジェクトにイメージファイルを与えなければならない今、以下のコードユニットテストの模擬HttpPostedFile

public void UpdateUserProfile_WithValidData() 
{ 
    HttpPostedFile httpPostedFile; 
    //httpPostedFile =?? 

    var returnObject = UpdateUserProfile(httpPostedFile); 

    //Assert code here 
} 

を持っています。単体テストで模擬画像をさらに進める方法を教えてください。

+0

については

は、この問題は解決されていますか? – Nkosi

+0

私のアプリケーションとしてNope @ Nkosiは特にHttpPostedFileを使用するので、HttpPostedFileBaseに変更しませんでした – thecrusader

答えて

0

HttpPostedFileは封印され、内部コンストラクタを持ちます。これはあなたの単体テストを模擬することを困難にします。

私はそれはあなたが相続を介して、またはモックフレームワークを経由して直接モックを作成できるようになる抽象クラスであるため、抽象HttpPostedFileBase

public async Task<HttpResponseMessage> UpdateUserProfile(HttpPostedFileBase postedFile) 
    //update operations 
} 

を使用するようにコードを変更助言します。 (部品番号を使用して)例

[TestMethod] 
public async Task UpdateUserProfile_WithValidData() { 
    //Arrange 
    HttpPostedFileBase httpPostedFile = Mock.Of<HttpPostedFileBase>(); 
    var mock = Mock.Get(httpPostedFile); 
    mock.Setup(_ => _.FileName).Returns("fakeFileName.extension"); 
    var memoryStream = new MemoryStream(); 
    //...populate fake stream 
    //setup mock to return stream 
    mock.Setup(_ => _.InputStream).Returns(memoryStream); 

    //...setup other desired behavior 

    //Act 
    var returnObject = await UpdateUserProfile(httpPostedFile); 

    //Assert 
    //...Assert code here 
} 
関連する問題