Moq VerifySet(动作)奇怪的行为

时间:2012-04-26 04:17:48

标签: moq

我使用Moq来测试某些行为。我想验证set view的model属性作为Message class的新实例:

[TestFixture]
public class MessageFormPresenterTests
{
    private NameValueCollection queryString;
    private Mock<IDatabase> mockDatabase;
    private Mock<HttpContextBase> mockHttpContext;
    private Mock<HttpRequestBase> mockRequest;
    private Mock<HttpResponseBase> mockResponce;
    private Mock<IMessageFormView> mockView;
    private MessageFormPresenter presenter;

    [SetUp]
    public void SetUp()
    {
        this.queryString = new NameValueCollection();
        this.mockDatabase = new Mock<IDatabase>();
        this.mockHttpContext = new Mock<HttpContextBase>();
        this.mockRequest = new Mock<HttpRequestBase>();
        this.mockResponce = new Mock<HttpResponseBase>();
        this.mockView = new Mock<IMessageFormView>();

        this.mockHttpContext.SetupGet(c => c.Request).Returns(this.mockRequest.Object);
        this.mockHttpContext.SetupGet(c => c.Response).Returns(this.mockResponce.Object);

        this.mockRequest.SetupGet(r => r.QueryString).Returns(this.queryString);

        this.presenter = new MessageFormPresenter(this.mockView.Object) { Database = this.mockDatabase.Object, HttpContext = this.mockHttpContext.Object };
    }

    [Test]
    public void ViewLoad_QueryStringNotHasMessageIdParameter_PopulateViewModelAsNewMessage()
    {
        // Act
        this.mockView.Raise(v => v.Load += null, new EventArgs());

        // Assert
        this.mockView.VerifySet(v => v.Model = It.Is<Message>(m => m.Id == 0));
    }
}

但即使我没有在MessageFormPresenter中编写此功能,此测试也会通过。当我将断点设置为动作调用(v.Model = It.Is(m =&gt; m.Id == 0))并调试我的测试时,Model属性返回为“Castle.Proxies.MessageProxy”类型并具有所有属性默认值(Id属性的值当然是0)。

为什么会这样?我没有设置任何返回,默认情况下Moq应该返回null。

P.S。对不起,我的英语很差。我希望你明白我在说什么)

UPDATE1:MessageFormPresenter类:

    public class MessageFormPresenter : Presenter<IMessageFormView>
{
    private IDatabase database;

    public MessageFormPresenter(IMessageFormView view)
        : base(view)
    {
        View.Load += this.View_Load;
    }

    public IDatabase Database
    {
        get { return this.database ?? (this.database = DatabaseFactory.DatabaseInstance); }
        set { this.database = value; }
    }

    private void View_Load(object sender, EventArgs e)
    {
        int messageId;
        var messageParam = Request.QueryString[QueryParamNames.MessageId];

        if (messageParam == null)
        {
            View.Model = new Message();
            return;
        }

        if (!View.IsAdmin)
        {
            Response.Redirect(PageUrls.AccessDenied, true);
            return;
        }

        if (int.TryParse(
            messageParam,
            NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
            CultureInfo.InvariantCulture,
            out messageId))
        {
            View.Model = this.Database.Single<Message>(messageId);
        }
        else
        {
            Response.Redirect(PageUrls.DefaultPage, true);
        }
    }
}

WebFormsMvp框架提供的基类Presenter<IMessageFormView>

UPDATE2:如果我使用严格行为,即使我设置了Mock<IMessageFormView>的所有属性,也会在MessageFormPresenter的基础构造函数中抛出TargetInvocationException。

0 个答案:

没有答案