2

So we're using C#/Specflow, and I have a test that reads

Given The publish directory can not be accessed

The app being tested reads/writes files from a directory, firstly checking it exists and if not throws an exception. I am testing that this exception is thrown. What I need to do is make the directory inaccessible for the duration of the test.

The options as I see it are:

  • Change the directory it's accessing by overriding it's config (Windows registry) for the duration of the test.

  • Rename the directory it's accessing for the duration of the test.

  • Change the permissions on the directory for the duration of the test.

None of these seem ideal, I'd like to leave the test server alone if possible. Can anyone tell me of a better solution to this please?

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
Russ Taylor
  • 360
  • 3
  • 10

1 Answers1

2

An option that you didn't specify is to mock out the file access behind an interface and then have the mock simulate lack of access.

This has the benefit of not needing to change anything for the test, but means that its not longer an integration tests. If you don't mock and instead change the config/folder access then this test will need to ensure it isn't run when any other tests are running as they might consequently fail due to those changes.

This question has an example of how you might do this simply, and also links this library and this library which might be able to help.

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • I think doing it in the mock is definitely an option. I have marked this as the answer as it seems a logical place to do it, thank you. My only concern is that is the mock truly representing the test scenario but that's probably down to the quality of the mocking and not a fundamental problem with doing it there. Actually the more I think about it, the more it seems the best option. – Russ Taylor Nov 19 '14 at 13:15
  • As it was, I ended up changing our registry configuration per test, and ensuring the test set up reset it. This seems a bit fragile though (e.g. I now have configuration set in the tests!) so I will change to the mocking plan. – Russ Taylor Nov 19 '14 at 13:20
  • 1
    For anyone else reading this I use https://github.com/tathamoddie/System.IO.Abstractions for mocking the filesystem. Thank you – Russ Taylor Nov 19 '14 at 13:52