0

I have a method that takes 2 parameters like:

assetService(assetDto dto, HttpPostedFileBase photo)

and i can't use moq with this. How can i do that? (using 'moq')


public ResultObjectDto CreateAsset(AssetDto model, HttpPostedFileBase file)

and i want to moq this

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

this moq is wrong, how can i do that

  • 1
    _and i can't use moq with this_ What do you mean? What are you actually trying to do? – Chris Mantle Jan 17 '14 at 09:57
  • i try to writing unit tests with moq. i have a file upload method that takes httppostedfileparameter but i can't fix it. there are many example with httppostedfile base but my method takes two parameter and i couldn't fix it. – Osman Yalçınkaya Jan 17 '14 at 13:04
  • Post an example of one of the unit tests. I still don't know what problem you're actually trying to solve. – Chris Mantle Jan 17 '14 at 14:28
  • I'm sorry, i was late. i update my question above – Osman Yalçınkaya Jan 29 '14 at 08:59
  • I'm also a little confused at what you are trying to do. Are you trying to mock the `HttpPostedFileBase`? – Jonny Jan 29 '14 at 09:16
  • Yes, the problem is "HttpPostedFileBase" – Osman Yalçınkaya Jan 30 '14 at 12:44
  • 1
    man literally the first result from google for "moq HttpPostedFileBase" is the solution http://stackoverflow.com/questions/15622153/mocking-httppostedfilebase-and-inputstream-for-unit-test Just Mock the HttpPostedFileBase and call the test exactly as you have in the Assert where the postedFileBase is the mock object. – Pedro.The.Kid Jan 31 '14 at 12:16

1 Answers1

1

First you have a class you want to mock with a method with several parameters

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

than to make a test you mock it

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));
}
Pedro.The.Kid
  • 1,968
  • 1
  • 14
  • 18