简体   繁体   English

Rhino Mocks没有嘲笑一种方法

[英]Rhino Mocks doesnt mock a method

Im trying to mock a method, it compiles without errors, but smth strange happens when i run a test. 我试图模拟一个方法,它编译没有错误,但是当我运行测试时,有些奇怪的事情发生了。 Actually method doesnt mock or maybe I dont understand smth...( 其实方法不会嘲笑,或者我可能听不懂...

Here's a code: 这是一个代码:

public class Robot
{   ....

    public virtual bool range(IObs ob, double range)
    {
        double dist = ob.distanceSq(this);
        if (dist < range)
            return true;
        else
            return false;
    }
}

... ...

public interface IObs
{
    double distanceSq(Robot r);
}

... ...

Unit Test: 单元测试:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        MockRepository mocks = new MockRepository();
        IObs obstacleMock = mocks.CreateMock<IObs>();
        Robot r = new Robot();
        Expect.Call(obstacleMock.distanceSq(r)).IgnoreArguments()
           .Constraints(Is.Anything())
            .Return(5.5);
        Assert.IsTrue(r.range(obstacleMock, 0.5));
    }
}

I mock distanceSq(). 我模拟distanceSq()。 When I debug my test, i see that ob.distanceSq(this) is 0.0. 当我调试测试时,我看到ob.distanceSq(this)是0.0。 (not 1.5). (不是1.5)。

What's wrong? 怎么了?

Your code does not actually use mock you created - as you can change Obstacle to IObjs as argument of range and than pass mock instead of new instance of Obstacle : 您的代码实际上并没有使用您创建的模拟-因为您可以将Obstacle更改为IObjs作为range参数,然后传递模拟而不是Obstacle的新实例:

public virtual bool range(IObjs ob, double range)....
class Obstacle : IObjs ...

Assert.IsTrue(r.range(obstacleMock, 0.5));

I would recommend that you upgrade your RinoMocks version to 3.6 and use the new syntax. 我建议您将RinoMocks版本升级到3.6,并使用新语法。

Additionally if you are using a mock for IObs, you may as well verify that the distanceSq method is called. 此外,如果将模拟用于IOb,则还可以验证是否调用了distanceSq方法。

I have left the verification of the parameters in the distanceSq method out of this code, but you could ensure that the correct Robot instance is passed in. 我没有在这段代码中保留对distanceSq方法中参数的验证,但是您可以确保传入正确的Robot实例。

The unit test will still fail however, as 5.5 is greater than 0.5 但是,单元测试仍将失败,因为5.5大于0.5

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Rhino
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var r = new Robot();
            var obstacleMock = MockRepository.GenerateMock<IObs>();
            obstacleMock.Expect(x => x.ditanceSq(Arg<Robot>.Is.Anything)).Return(5.5);
            //use this line to ensure correct Robot is used as parameter
            //obstacleMock.Expect(x => x.ditanceSq(Arg<Robot>.Is.Equal(r))).Return(5.5);
            var result = r.range(obstacleMock, 0.5);
            obstacleMock.VerifyAllExpectations();
            Assert.IsTrue(result);
        }
    }

    public class Robot
    {
        public virtual bool range(IObs ob, double range)
        {
            return  ob.ditanceSq(this) < range;         
        }
    }

    public interface IObs
    {
        double ditanceSq(Robot r);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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