Mike Cole
Mike Cole

Reputation: 14713

Unit test if method deletes a source data file

Super noob with unit testing...

I have a method that processes and then deletes an input file. How can I unit test the if the file actually gets deleted? I understand it's a bad practice to do actual file IO during a unit test.

Thanks!

Upvotes: 1

Views: 207

Answers (2)

Gishu
Gishu

Reputation: 136613

It's not an absolute rule - if you're class lives on the periphery i.e. interfaces with some external subsystem (in this case the filesystem), then the right way to test it is with the real subsystem.

The GOOS book deems these tests as "integration tests" because these tests test whether your class integrates with the external subsystem.

The consumers of this class can abstract away the file system via an interface exposed by this class.

My question/acid test for this decision is - what is the primary responsibility of this class ?

  • if it is accessing the filesystem, then I need to write integration tests
  • if it is not, then use a mock and concentrate on the real responsibility. e.g. act as a customer repository (which "uses/delegates" to a dependency to deal with the filesystem).

Upvotes: 1

TyCobb
TyCobb

Reputation: 9089

Have the method do a System.IO.File.Copy() of the source file and then use the copy to do your testing. Hopefully, your code is setup to specify which file to process.

Upvotes: 0

Related Questions