开发者

MVP with Moq - loading a mocked view

I've read a lot about mocking/stubbing/faking - and still hit my mental roadblocks.

I'm trying to adapt MVP (Model View Presenter) with a "fun" weight loss tracking system I'm building for my own Fatty McFatter-self. I'm trying to TDD & 'by the book' this but hit many mental blocks and stall out.

I am building my Presenter and mocking my Service & View at the moment. Here's my test: again note: service and view are mocked with Moq

[Test]
    public void GetLog_WithExistingDate_ViewSetWithExistingLog()
    {
        WeightLogModel model = new WeightLogModel
                                   {
                                       EntryDate = DateTime.Now,
                                       Waist = 42,
                                       Weight = 242
                                   };

        service.Setup(x => x.GetLog(It.IsAny<DateTime>())).Returns(model);

        presenter.Display(DateTime.Now);

        IWeightLogView myView = view.Object;
        Assert.AreEqual(model.Weight, myView.Weight);

    }

and in my Presenter - this is my Display method:

public void Display(DateTime date)
    {
        var wei开发者_如何学JAVAghtLog = service.GetLog(date);
        if(weightLog == null) return;

        View.EntryDate = weightLog.EntryDate;
        View.Waist = weightLog.Waist;
        View.Weight = weightLog.Weight;
    }

Now - if I debug as Display is being called - I see the weightLog is filled with the correct info I've setup in the mock. But as it's suppose to set View.EntryDate, View.Waist, etc - the View values never change. They stay zero or 0001/1/1

Is there some way to make it work? Or is this just a bad test and I'm floundering in confusion?


Thanks to Phil for starting me in motion. Although I didn't want to explicitly set what I was going to return - I wanted the mock view to behave like my view. You can have the mocked setter behave as normal by calling SetupProperty --> view.SetupProperty(x => x.Weight) //in my case... here's the test that will now pass asserting the weight was set

[Test]
    public void GetLog_WithExistingDate_ViewSetWithExistingLog()
    {
        WeightLogModel model = new WeightLogModel
                                   {
                                       EntryDate = DateTime.Now,
                                       Waist = 42,
                                       Weight = 242
                                   };

        service.Setup(x => x.GetLog(It.IsAny<DateTime>())).Returns(model);
        // I ADDED THIS ONE LINE
        view.SetupProperty(x => x.Weight);

        presenter.Display(DateTime.Now);

        IWeightLogView myView = view.Object;
        Assert.AreEqual(model.Weight, myView.Weight);

    }


You are not showing all your setup code here, nor the dependencies between classes.

However if you are indeed mocking the view called "myView", it's going to return what you have the mock set up to return, or defaults for each type if you haven't specified anything for it to return (which sounds like what is happening).

From your comment:

I am trying to setup the service.GetLog(date) to return the WeightLogModel I have in the test. My thinking is that doing so - would make that WeightLogModel available in my presenter

So far that seems like it is working from your original question.

to assign to my mocked view - where View.EntryDate = weightLog.EntryDate .... in this case weightLog is what is setup in the test.... I hope I'm clear as to where my head is... I'm not saying I'm right - this is what my thinking is though.

Where are you going wrong is where you say "to assign to my mocked view". It's not clear from your code whether or not the View property is in fact your mocked view (because your code is incomplete).

Although, in this case, it actually doesn't matter. If the View property is in fact a mock, it will only return what you tell it to return--its properties are not going to behave like "normal" properties.

So the following will fail without explicit setup:

mockView.MyProperty = "hello";
Assert.AreEqual("hello", mock.MyProperty);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜