【问题标题】:Using Moq with HttpPostedFileBase and one other parameter将 Moq 与 HttpPostedFileBase 和另一个参数一起使用
【发布时间】:2014-01-17 09:37:31
【问题描述】:

我有一个采用 2 个参数的方法,例如:

assetService(assetDto dto, HttpPostedFileBase photo)

我不能用这个最小起订量。我怎样才能做到这一点? (使用“起订量”)


public ResultObjectDto CreateAsset(AssetDto model, HttpPostedFileBase file)

我想最小起订量

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

这个起订量不对,我该怎么办

【问题讨论】:

  • 我不能用这个最小起订量你是什么意思?你到底想做什么?
  • 我尝试使用最小起订量编写单元测试。我有一个采用 httppostedfile 参数的文件上传方法,但我无法修复它。 httppostedfile 基础有很多例子,但我的方法有两个参数,我无法修复它。
  • 发布一个单元测试的例子。我还是不知道你到底想解决什么问题。
  • 对不起,我迟到了。我更新了我上面的问题
  • man 从字面上看,谷歌“moq HttpPostedFileBase”的第一个结果是解决方案stackoverflow.com/questions/15622153/… 只需模拟 HttpPostedFileBase 并完全按照您在 PostedFileBase 是模拟对象的断言中调用测试。

标签: c# moq httppostedfilebase


【解决方案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));
}

【讨论】:

  • HttpPostedFile 怎么样?
  • 基本上你设置所有方法和属性来返回你期望从 HttpPostedFile 对象中得到的测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多