Export to GitHub

moq - issue #315

Moq does not recognize Setup with List<string>


Posted on Jul 7, 2011 by Helpful Giraffe

What steps will reproduce the problem? The View interface: public interface IView { void SetList(IList<string> stringList); }

The implementing View: public partial class Form1 : Form, IView { public Form1() { InitializeComponent(); }

public void SetList(IList&lt;string&gt; stringList)
{
    //
}

}

The view presenter: public sealed class ViewPresenter { private readonly IView view;

public ViewPresenter(IView view)
{
    this.view = view;

    var list = new List&lt;string&gt;();
    list.Add(&quot;item1&quot;);
    list.Add(&quot;item2&quot;);

    this.view.SetList(list);
}

}

The test: [TestClass] public class ViewPresenterFixture { private ViewPresenter testee; private Mock<IView> mockView; private IList<string> stringList;

[TestInitialize]
public void TestInitialize()
{
    this.mockView = new Mock&lt;IView&gt;(MockBehavior.Strict);

    this.stringList = new List&lt;string&gt;();
    this.stringList.Add(&quot;Item1&quot;);
    this.stringList.Add(&quot;Item2&quot;);

    this.mockView.Setup(mock =&gt; mock.SetList(this.stringList));
}

[TestMethod]
public void TestMethod1()
{
    this.testee = new ViewPresenter(this.mockView.Object);            
}

}

What is the expected output? What do you see instead? Moq should not fire

Test method MoqTest.Test.ViewPresenterFixture.TestMethod1 threw exception: Moq.MockException: IView.SetList(System.Collections.Generic.List`1[System.String]) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.

What version of the product are you using? On what operating system? Moq 4.0.10827.0, MSTest, Visual Studio 2010 Ultimate, Windows 7 64-Bit

Please provide any additional information below.

Comment #1

Posted on Jul 7, 2011 by Quick Monkey

Your class under test (the presenter) is doing:

    var list = new List<string>();

Hence, that list will never be the same that you setup for your mock. You need to use It.Is<> or It.IsAny<>.

Could you please send these questions to the discusion mailing list or stackoverflow instead of creating issues?

Status: Invalid

Labels:
Type-Defect Priority-Medium