开发者

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 t开发者_运维知识库o do actual file IO during a unit test.

Thanks!


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜