2008 Advent Calendar December 21st




1: public class Advent21
2: {
3: private FileUtilWithDelete Given_A_Readable_File(string content)
4: {
5: FileUtilWithDelete file = new FileUtilWithDelete(“SomeFile.txt”);
6: file.Create(content);
7: return file;
8: }
9:  
10: private FileUtilWithDelete Given_An_Unreadable_File()
11: {
12: FileUtilWithDelete file = Given_A_Readable_File(“SHOULD NOT BE ABLE TO READ THIS”);
13: file.Readable = false;
14: return file;
15: }
16:  
17: [Fact]
18: public void Reading_A_Readable_File_Returns_File_Content()
19: {
20: using (FileUtilWithDelete file = Given_A_Readable_File(“CONTENT”))
21: {
22: string content = file.Read();
23: Assert.Equal<string>(“CONTENT”, content);
24: }
25: }
26:  
27: [Fact]
28: public void Reading_An_Unreadable_File_Throws_Correct_Exception()
29: {
30: using (FileUtilWithDelete file = Given_An_Unreadable_File())
31: {
32: Assert.Throws<AccessViolationException>(() => { file.Read(); });
33: }
34: }
35: }

Now let’s consider the name of the class containing all tests. A pretty bad name if you ask me. And since we’ve used the “given” pattern from BDD we can use BDD to inspire us into a better “test class name”.

Comments (0)