You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to mock a situation where a file cannot be read and an IOException is thrown. Right now, I have to use Moq and
Mock the IFileSystem
Mock it's File property
Mock all methods on the File object that will be called by my code and have them return valid data for some inputs and throw an exception for others.
This is very cumbersome and tests break as soon as I change my code to use different methods on File.
Instead, I'd like to just mock the MockFileData#Content property and have that throw an IOException. However, that can't be mocked because the property is not virtual and there's no interface for MockFileData I could instead mock against.
Solution: either make all relevant properties of MockFileData virtual so they can be mocked or create an interface for MockFileData and use that everywhere instead of the concrete class.
My specific use case was mocking permission problems (can't read or write the file). But I can see many other types of exceptions as well like the ones you guys mention. That's why I don't think IsCorrupted is the best solution as it wouldn't let me control the type of exception being thrown. I'd rather be able to provide my own exception
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
ericnewton76 commentedon Jun 25, 2018
Seems reasonable.
What's your base IOException issue? Was it disk write failure or something?
tathamoddie commentedon Jul 4, 2018
Or we add a
MockFileData.IsCorruptproperty, that lets it exhibit corrupt behaviour for you?karottenreibe commentedon Jul 6, 2018
My specific use case was mocking permission problems (can't read or write the file). But I can see many other types of exceptions as well like the ones you guys mention. That's why I don't think IsCorrupted is the best solution as it wouldn't let me control the type of exception being thrown. I'd rather be able to provide my own exception
fgreinacher commentedon Jul 11, 2018
@karottenreibe Would #144 help solve your use case?
I don't like making everything
virtualjust for the sake of being able to mock it somehow.github-actions commentedon Jan 19, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.