Skip to content

MockFileData should be mockable #288

Closed

Description

@karottenreibe

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.

Activity

  1. ericnewton76 commented on Jun 25, 2018

    @ericnewton76
    Contributor

    Seems reasonable.

    What's your base IOException issue? Was it disk write failure or something?

  2. tathamoddie commented on Jul 4, 2018

    @tathamoddie
    Contributor

    Or we add a MockFileData.IsCorrupt property, that lets it exhibit corrupt behaviour for you?

  3. karottenreibe commented on Jul 6, 2018

    @karottenreibe
    Author

    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

  4. fgreinacher commented on Jul 11, 2018

    @fgreinacher
    Contributor

    @karottenreibe Would #144 help solve your use case?

    I don't like making everything virtual just for the sake of being able to mock it somehow.

  5. added and removed on Mar 24, 2019
  6. github-actions commented on Jan 19, 2020

    @github-actions

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions