简体   繁体   中英

How do i express time correlation of two Rhino mock calls

I have a device that needs to be mocked. Depending on the last set value a statusflag on the device changes. I need to mock this behaviour.

Here is some pseudocode that does not work:

private delegate void setvalue(Value val);
...
Value lower = new Value(1);
Value higher = new Value(7);
ISweetInterface mock = setupSweetInterface();
Boolean flagreturn = false;
mock.Expect(x => x.Lower).Return(lower);
mock.Expect(x => x.SetValue(lower))
    .Do(new setvalue(delegate(Value val) { flagreturn = true; }));
mock.Expect(x => x.SetValue(higher))
    .Do(new setvalue(delegate(Value val) { flagreturn = false; }));

mock.Expect(x => x.CheckFlag()).Return(flagreturn);

Mockuser tested = new Mockuser(mock)
Assert.IsTrue(tested.setLowerAndCheckFlag());
Assert.IsFalse(tested.setHigherAndCheckFlag());

Depending on the last set value the flag on the device has to change. I think my delegate actually can not see the flagreturn. The Code i wrote compiles but CheckFlag() always returns false.

How can i properly express this coupled behaviour.

I got it to work with the assistance of another object that keeps track for me. One of the cornerstones is to invoke the new object with DO rather than just trying to return one of its properties.

Here is some pseudocode to illustrate:

    private class mockdevice
    {
        public List<Value> listOfTrueReturn = new List<Value>();
        private Boolean flagvalue = false;
        public bool flag()
        {
                return flagvalue;
        }
        public void setvalue(Value val)
        {
            if (listOfTrueReturn.Contains(val))
            {
                flagvalue = true;
            }
            else
            {
                flagvalue = false;
            }
        }
    }


    private delegate void setvalue(Value val);
    private delegate Boolean getflag();

    [Test]
    public void shouldAnswerStackoverflowQuestion()
    {
        float d = 5f;
        Value lower = new Value(-d, 0f, unit);
        Value higher = new Value(d, 0f, unit);
        mockdevice md = new mockdevice();
        md.listOfTrueReturn.Add(higher);

        IFindFlagDataObject mock = setupmock();
        mock.Expect(x => x.Lower).Return(lower);
        mock.Expect(x => x.Higher).Return(higher);
        mock.Expect(x => x.CheckFlag()).Do(new getflag(md.flag)).Repeat.Times(2);
        mock.Expect(x => x.SetValue(Arg<Value>.Is.Anything)).Do(new setvalue(md.setvalue)).Repeat.Times(2);

        FindFlagStrategy tested = new FindFlagStrategy(mock);
        tested.FindFlagValue();

        mock.VerifyAllExpectations();
        Mockuser tested = new Mockuser(mock)
        Assert.IsTrue(tested.setLowerAndCheckFlag());
        Assert.IsFalse(tested.setHigherAndCheckFlag());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM