O.O
O.O

Reputation: 11307

How to NMock FileStream?

I have this code in my unit test:

var file = m_Mockery.NewMock<IFile>();
Stream s = new MemoryStream();

Expect.Once.On( file ).Method( "OpenRead" )
.With( "someFile.mdb")
.Will( Return.Value( s ) );
...
...
...

// this runs the real code that contains the OpenRead call
productionCodeObj.DoIt("someFile.mdb");
m_Mockery.VerifyAllExpectationsHaveBeenMet();

The problem is when I call DoIt (which calls OpenRead), I get an exception saying the file cannot be found. Am I misunderstanding what nmock does? I don't want my unit test to hit the real filesystem...

Upvotes: 2

Views: 221

Answers (1)

David Tischler
David Tischler

Reputation: 2682

Yes, I think you are misunderstanding what NMock does. It is used to create objects that you pass into your object under test, so that they act upon those mock objects. As it seems that your "productionCodeObj" news up a FileStream instance itself, so it won't use your mock. You would have to pass file into it.

Upvotes: 2

Related Questions