如何使用Rhino Mocks捕获Stream属性的值?

时间:2012-05-29 14:13:52

标签: c# unit-testing properties rhino-mocks

[TestClass]
public class FooTests {
        [TestMethod]
        public void TestFoo() {
            var fooMock = MockRepository.GenerateMock<IFoo>();
            // MUT
            FooUser.Run(fooMock);

            var stream = fooMock.Content;
            stream.Position = 0;

            var first = stream.ReadByte();
            Assert.AreEqual(0x77, (byte) first);
            var second = stream.ReadByte();
            Assert.AreEqual(0x78, (byte) second);
        }
    }

    public class Foo :IFoo {
        public Stream Content { get; set; }
    }

    public class FooUser {
        public static void Run(IFoo foo) {
            foo.Content = new MemoryStream(new byte[] {0x77, 0x78});
        }
    }

    public interface IFoo {
        Stream Content { get; set; }
    }

AssertWasCalled似乎不适合验证流。

fooMock.Content.Stub(aStream => stream = aStream);

抛出异常:

System.ArgumentNullException: You cannot mock a null instance
Parameter name: mock
  

注意:似乎Moles会更好地解决这个问题。

1 个答案:

答案 0 :(得分:1)

替换:

var fooMock = MockRepository.GenerateMock<IFoo>();

使用:

var fooMock = MockRepository.GenerateStub<IFoo>();