Search code examples
c#unit-testingpropertiesrhino-mocks

How do I capture the value of a Stream property using 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 does not seem to be a good fit for validating the stream.

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

threw exception:

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

Note: Seems like Moles would work better for this problem.


Solution

  • Replace:

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

    with:

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