C # Rhino mocks - Is this a suitable use of mocks?

Forgive me for my ignorance. I am trying to learn Rhino.

public Foo(Stream stream)
{
    if (stream == null) throw new ArgumentNullException("woot");
    if (!stream.CanRead) throw new NotSupportedException("sporkish");
    if (!stream.CanSeek) throw new NotSupportedException("monkey");
}

I would like to test this feature with the NUnit test by doing a check for these exceptions. Is this a suitable use for mock objects, or do I really need to create a special inherited Stream class? If taunts, then how to do it using Rhino? I can figure out how to return dummy values ​​from functions (I think), but not properties.

+3
source share
2 answers

You can create a layout Streamthat indicates that it cannot read and cannot search, as shown below.

MockRepository mocks = new MockRepository();

Stream mockStream = mocks.StrictMock<Stream>();

Expect.Call(mockStream.CanRead).Return(false);
Expect.Call(mockStream.CanSeek).Return(false);

mocks.ReplayAll();

// Perform the unit test.

mocks.VerifyAll();

, :

  • Mocking - , . , . , . , CanRead CanSeek VerifyAll(), , .
  • Mocking . , . Stream , , . / , .

, , : , (!stream.CanRead) , (!stream.CanSeek).

+5

Mocks. Mock. stream.CanRead, stream.CanSeek ( ).

, Stream, ( ).

+4

Source: https://habr.com/ru/post/1726926/


All Articles