Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FileRegionsCache testability #2129

Closed
ghost opened this issue Oct 25, 2020 · 3 comments · Fixed by #2131
Closed

Improve FileRegionsCache testability #2129

ghost opened this issue Oct 25, 2020 · 3 comments · Fixed by #2131
Assignees
Labels
area-test Issues for improving the tests and adding new ones. design-improvement

Comments

@ghost
Copy link

ghost commented Oct 25, 2020

I'm writing some unit tests for the VS Extension that require the use of a FileRegionsCache object, a concrete class that interacts with the file system. I can create, and use in my tests, a FileRegionsCache object with a mock IFileSystem. But that requires me to understand the internal workings of FileRegionsCache so I can mock the correct set of file system calls, and I also have to provide fake file system content to the mock.

What I want/need instead is an IFileRegionsCache interface that I can mock, so that when the VS Extension product code that I'm unit testing calls fileRegionsCache.PopulateTextRegionProperties, I can directly return the value I want.

@michaelcfanning @eddynaka @GabeDeBacker FYI

@ghost ghost added design-improvement area-test Issues for improving the tests and adding new ones. labels Oct 25, 2020
@eddynaka
Copy link
Collaborator

eddynaka commented Oct 25, 2020

Hi @lgolding ,

I was taking a look at this. I can expose the interface, but i will have to include one more method if that's not a problem. Below the interface:

public interface IFileRegionsCache
    {
        Region PopulateTextRegionProperties(Region inputRegion, Uri uri, bool populateSnippet);

        Region ConstructMultilineContextSnippet(Region inputRegion, Uri uri);
    }

Today the ConstructMultilineContextSnippet method is internal and I would have to move to public.

@michaelcfanning
Copy link
Member

I'm not a big fan of using interfaces exclusively to drive testability. This results in a certain amount of type proliferation which generally only serves a single purpose (to help with testing). A more appropriate use of interfaces supports arbitrary additional uses.

What tends to happen in projects like this one is that the interfaces are created in a somewhat haphazard manner and applied similarly (i.e., the abstraction is pushed through the API only in reaction to whatever testing is done). Broad areas of the code base don't have this kind of interface use, with the result that the API itself isn't consistent.

My suggestion here is simply to ensure that FileRegionsCache is public, unsealed and to apply the 'virtual' keyword to relevant methods. This can be a simple, clean approach that allows easier testing. This approach can break down when attempting to test objects that are created indirectly by other classes/API but that doesn't seem to be the case here.

@michaelcfanning michaelcfanning changed the title Provide an interface IFileRegionsCache Improve FileRegionsCache testability Oct 25, 2020
@eddynaka
Copy link
Collaborator

I'm not a big fan of using interfaces exclusively to drive testability. This results in a certain amount of type proliferation which generally only serves a single purpose (to help with testing). A more appropriate use of interfaces supports arbitrary additional uses.

What tends to happen in projects like this one is that the interfaces are created in a somewhat haphazard manner and applied similarly (i.e., the abstraction is pushed through the API only in reaction to whatever testing is done). Broad areas of the code base don't have this kind of interface use, with the result that the API itself isn't consistent.

My suggestion here is simply to ensure that FileRegionsCache is public, unsealed and to apply the 'virtual' keyword to relevant methods. This can be a simple, clean approach that allows easier testing. This approach can break down when attempting to test objects that are created indirectly by other classes/API but that doesn't seem to be the case here.

just created a second PR with this approach + capacity fix + docs update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-test Issues for improving the tests and adding new ones. design-improvement
Projects
None yet
2 participants