1

我有一个struct我想嘲笑的。在更复杂的测试中,我需要这个结构的几个实例,每个实例都有自己的行为。为此,我创建了一个辅助方法。

private MyStruct CreateMock(string toString) {
    var mock = Mock.Create<MyStruct>();
    Mock.Arrange(() => mock.toString()).Returns(toString);
    return mock;
}

当我调试多次调用此方法的测试时,似乎该Arrange调用已被结构的所有实例覆盖(或者我使用的是结构模拟而不是实例模拟?)。

我试过了:

mock.Arrange(m => m.toString()).Returns(toString); // Using Helpers assembly, note the lowercase m at the start of the line.

但无济于事。如何获取结构的多个实例?

我正在使用:Microsoft Visual Studio Enterprise 2017 版本 15.9.17 VisualStudio.15.Release/15.9.17+28307.905 Microsoft .NET Framework 版本 4.8.03761

安装版本:企业版

JustMock 2020.1.219.1 Telerik JustMock 扩展。

添加示例

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;

namespace JustMockFramework
{
    public struct MyStruct
    {
        public readonly string Id;

        public MyStruct(string id)
        {
            Id = id;
        }

        public new string ToString()
        {
            return "Never read me!";
        }
    }

    [TestClass]
    public class MWE
    {
        [TestMethod]
        public void TestSimpleStruct()
        {
            var simpleTest = new MyStruct("1");

            Assert.AreEqual("Never read me!", simpleTest.ToString());
        }

        [TestMethod]
        public void TestMockOfStruct()
        {
            var mock = Mock.Create<MyStruct>();
            Mock.Arrange(() => mock.ToString()).Returns("Read me!");

            Assert.AreEqual("Read me!", mock.ToString());
        }

        [TestMethod]
        public void TestTwoMocksOfStruct()
        {
            var firstMock = Mock.Create<MyStruct>();
            Mock.Arrange(() => firstMock.ToString()).Returns("Read me!");

            var secondMock = Mock.Create<MyStruct>();
            Mock.Arrange(() => secondMock.ToString()).Returns("Read me too!");

            Assert.AreEqual("Read me!", firstMock.ToString()); // Fails with "Read me too!"
            Assert.AreEqual("Read me too!", secondMock.ToString());
        }

        [TestMethod]
        public void TestTwoMocksOfStructCreatedInHelper()
        {
            var firstMock = CreateMockOfStruct("Read me!");

            var secondMock = CreateMockOfStruct("Read me too!");

            Assert.AreEqual("Read me!", firstMock.ToString()); // Fails with "Read me too!"
            Assert.AreEqual("Read me too!", secondMock.ToString());
        }

        private MyStruct CreateMockOfStruct(string toString)
        {
            var mock = Mock.Create<MyStruct>();
            Mock.Arrange(() => mock.ToString()).Returns(toString);
            return mock;
        }

        [TestMethod]
        public void TestTwoMocksOfStructCreatedInHelperWithHelper()
        {
            var firstMock = CreateMockOfStructWithHelper("Read me!");

            var secondMock = CreateMockOfStructWithHelper("Read me too!");

            Assert.AreEqual("Read me!", firstMock.ToString()); // Fails with "Read me too!"
            Assert.AreEqual("Read me too!", secondMock.ToString());
        }

        private MyStruct CreateMockOfStructWithHelper(string toString)
        {
            var mock = Mock.Create<MyStruct>();
            mock.Arrange((m) => m.ToString()).Returns(toString);
            return mock;
        }
    }
}

编辑:交叉发布

我已经在 Telerik JustMock 论坛上交叉发布了这个问题

编辑:许可证扩展

我的试用许可证被慷慨地延长了。我已经更新了答案以反映这一点。

4

3 回答 3

1

如问题中所述,我交叉发布了问题并在那里得到了答案。

这一切都归结为价值与参考比较。我假设进行了参考比较,但使用了值比较。当实际将 an 传递id给模拟的创建时,它应该可以工作。

为方便起见,请提供 Telerik 提出的解决方案的副本。

public struct MyStruct
{
    public readonly string Id;

    public MyStruct(string id)
    {
        Id = id;
    }

    public override string ToString()
    {
        return "Never read me!";
    }
}

[TestClass]
public class MWE
{
    [TestMethod]
    public void TestSimpleStruct()
    {
        var simpleTest = new MyStruct("1");

        Assert.AreEqual("Never read me!", simpleTest.ToString());
    }

    [TestMethod]
    public void TestMockOfStruct()
    {
        var mock = Mock.Create<MyStruct>("1");
        Mock.Arrange(() => mock.ToString()).Returns("Read me!");

        Assert.AreEqual("Read me!", mock.ToString());
    }

    [TestMethod]
    public void TestTwoMocksOfStruct()
    {
        var firstMock = Mock.Create<MyStruct>("1");
        Mock.Arrange(() => firstMock.ToString()).Returns("Read me!");

        var secondMock = Mock.Create<MyStruct>("2");
        Mock.Arrange(() => secondMock.ToString()).Returns("Read me too!");

        Assert.AreEqual("Read me!", firstMock.ToString()); // Fails with "Read me too!"
        Assert.AreEqual("Read me too!", secondMock.ToString());
    }

    [TestMethod]
    public void TestTwoMocksOfStructCreatedInHelper()
    {
        var firstMock = CreateMockOfStruct("1", "Read me!");
        var secondMock = CreateMockOfStruct("2", "Read me too!");

        Assert.AreEqual("Read me!", firstMock.ToString()); // Fails with "Read me too!"
        Assert.AreEqual("Read me too!", secondMock.ToString());
    }

    private MyStruct CreateMockOfStruct(string id, string toString)
    {
        var mock = Mock.Create<MyStruct>(id);
        Mock.Arrange(() => mock.ToString()).Returns(toString);
        return mock;
    }

    [TestMethod]
    public void TestTwoMocksOfStructCreatedInHelperWithHelper()
    {
        var firstMock = CreateMockOfStructWithHelper("1", "Read me!");
        var secondMock = CreateMockOfStructWithHelper("2", "Read me too!");

        Assert.AreEqual("Read me!", firstMock.ToString()); // Fails with "Read me too!"
        Assert.AreEqual("Read me too!", secondMock.ToString());
    }

    private MyStruct CreateMockOfStructWithHelper(string id, string toString)
    {
        var mock = Mock.Create<MyStruct>(id);
        mock.Arrange((m) => m.ToString()).Returns(toString);
        return mock;
    }
}
于 2020-04-17T12:27:33.513 回答
0

利用InSequence()

例如:

var firstMock = Mock.Create<MyStruct>();
Mock.Arrange(() => firstMock.ToString()).Returns("Read me!").InSequence();

var secondMock = Mock.Create<MyStruct>();
Mock.Arrange(() => secondMock.ToString()).Returns("Read me too!").InSequence();
于 2020-04-15T11:38:48.953 回答
0

对于这些类型的场景,您需要使用 Autofixture。 https://github.com/AutoFixture/AutoFixture

于 2020-04-17T11:35:20.573 回答