2014-01-17 31 views
0

我有需要兩個參數等的方法:使用起訂量與HttpPostedFileBase和一個其他參數

assetService(assetDto dto, HttpPostedFileBase photo) 

,我無法與此使用MOQ。我怎樣才能做到這一點? (使用 'MOQ')


public ResultObjectDto CreateAsset(AssetDto model, HttpPostedFileBase file) 

,我想MOQ這個

Assert.IsTrue(_assetService.CreateAsset(new AssetDto(), postedFileBase).ResultType == ResultType.Error); 

這起訂量是錯誤的,我怎麼能做到這一點

+1

_and我不能用moq與this_你是什麼意思?你究竟在做什麼? –

+0

我嘗試用moq編寫單元測試。我有一個文件上傳方法,需要httppostedfileparameter,但我無法修復它。有很多基於httppostedfile的例子,但我的方法需要兩個參數,我無法修復它。 – Osman

+0

發佈其中一個單元測試的示例。我仍然不知道你真的想要解決什麼問題。 –

回答

1

首先,你有你想要的一類模擬一個有幾個參數的方法

public class foo 
{ 
    public virtual int bar(int num, string str, bool b) 
    { 
     return 1; 
    } 
} 

比做一個測試你嘲笑它

public void TestMethod1() 
{ 
    //Mock of the foo class 
    var t = new Mock<foo>(MockBehavior.Strict); 

    //Setup to return what we want 0 instead of 1 
    t.Setup(e => e.bar(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<bool>())) 
        .Returns((int i, string s, bool b) => { return 0; }); 

    //the actual object 
    var f = t.Object; 
    //the actual test 
    Assert.AreEqual(0, f.bar(1, "s", false)); 
} 
+0

HttpPostedFile如何? – Osman

+0

基本上你設置了所有的方法和屬性來返回你期望從HttpPostedFile對象進行測試。 –