0

Can you please tell the scenario when the object returned could be different from the setup object?

The Object Id set during debugging in Setup is different when src code is executed.

interface IBaseClass
{
    IFactory aFactory;
}

class Point
{
    Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    int x;
    int y;
}

interface Factory
{
    public AnObject create(string a, Point pt);
}

Test Code

var anObjectMock = new Mock<AnObject> { DefaultValue = DefaultValue.Mock };

var sMatcher = It.Is<string>(s => s.Equals("aString"));
var ptMatcher = It.Is<Point>(p => (p.x == CONST_X && p.y == CONST_Y));

var FactoryMock = Mock.Get<IFactory>(IBaseClassMock.Object.Factory);
FactoryMock.Setup(f => f.create(sMatcher, ptMatcher)).Returns(anObjectMock.Object); // Say ObjectId is #1

SRC

Object obj = BasicImaging.Factory.create("aString", new Point(CONST_X, CONST_Y)); 
// Getting no ObjectId
4

2 回答 2

0

BasicImaging.Factory实际的模拟 ( IBaseClassMock.Factory) 吗?我想不是。

您需要将您的模拟注入BasicImaging.Factory其中,以便它使用您的模拟而不是实际代码。

于 2013-03-12T11:45:33.657 回答
0

而不是BasicImaging.Factory你应该使用IBaseClassMock.Object.Factory(即你设置的模拟)。

于 2013-03-12T11:45:56.480 回答