0
[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.

Kevin Driedger
  • 51,492
  • 15
  • 48
  • 55

1 Answers1

2

Replace:

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

with:

var fooMock = MockRepository.GenerateStub<IFoo>();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928