Being Cellfish

Stuff I wished I've found in some blog (and sometimes did)

December, 2008

Change of Address
This blog has moved to blog.cellfish.se.
Posts
  • Being Cellfish

    2008 Advent Calendar December 5th

    • 2 Comments
    
    
    1: public class Advent5 : IDisposable 2: { 3: private IFileUtil m_file; 4:   5: public void Dispose() 6: { 7: m_file.Delete(); 8: } 9:   10: [Fact] 11: public void TestReadOK() 12: { 13: // Create a test file 14: m_file = new FileUtil("SomeFile.txt"); 15: m_file.Create("CONTENT"); 16:   17: // Should be able to read file 18: Assert.DoesNotThrow(() => { m_file.Read(); }); 19: } 20:   21: [Fact] 22: public void TestReadFails() 23: { 24: // Create a test file 25: m_file = new FileUtil("SomeFile.txt"); 26: m_file.Create("CONTENT"); 27:   28: m_file.Readable = false; 29:   30: // Should NOT be able to read file. 31: Assert.Throws<AccessViolationException>(() => { m_file.Read(); }); 32: } 33: }

    Now we're using Xunit-net's clean up mechanism, the IDisposableinterface. We still got some redundant code in the setup part of each test. Next I'll refactor that.

  • Being Cellfish

    2008 Advent Calendar December 4th

    • 0 Comments
    
    
    1: public class Advent4 2: { 3: [Fact] 4: public void TestReadOK() 5: { 6: // Create a test file 7: IFileUtil file = new FileUtil("SomeFile.txt"); 8: file.Create("CONTENT"); 9:   10: // Should be able to read file 11: Assert.DoesNotThrow(() => { file.Read(); }); 12:   13: // Clean up 14: file.Delete(); 15: } 16:   17: [Fact] 18: public void TestReadFails() 19: { 20: // Create a test file 21: IFileUtil file = new FileUtil("SomeFile.txt"); 22: file.Create("CONTENT"); 23:   24: file.Readable = false; 25:   26: // Should NOT be able to read file. 27: Assert.Throws<AccessViolationException>(() => { file.Read(); }); 28:   29: // Clean up 30: file.Delete(); 31: } 32: }

    Now that the initial test has been split up into two tests. One testing reading a readable file and one testing an unreadable file. Now I got some redundant code I wanna get rid of. The first thing is the clean up code. It is not run if the test fails and that is probably a bad thing. So next I'll fix that.

  • Being Cellfish

    2008 Advent Calendar December 3rd

    • 0 Comments
    
    
    1: public class Advent3 2: { 3: [Fact] 4: public void TestRead() 5: { 6: // Create a test file 7: IFileUtil file = new FileUtil("SomeFile.txt"); 8: file.Create("CONTENT"); 9:   10: // Should be able to read file 11: Assert.DoesNotThrow(() => { file.Read(); }); 12:   13: file.Readable = false; 14:   15: // Should NOT be able to read file. 16: Assert.Throws<AccessViolationException>(() => { file.Read(); }); 17:   18: // Clean up 19: file.Delete(); 20: } 21: }

    Using XUnit.net asserts to verify exceptions is much better than what we did yesterday. Now the test starts to be small enough to be understood easily. But I think there is a major problem with this test. We're not only testing that we cannot read an unreadable file, I'm also verifying that I can read a readable file. I think this should be split up into two different tests and that is what will happen next.

  • Being Cellfish

    2008 Advent Calendar December 2nd

    • 0 Comments
    
    
    1: public class Advent2 2: { 3: [Fact] 4: public void TestRead() 5: { 6: // Create a test file 7: IFileUtil file = new FileUtil("SomeFile.txt"); 8: file.Create("CONTENT"); 9:   10: // Should be able to read file 11: try 12: { 13: file.Read(); 14: } 15: catch (Exception) 16: { 17: throw new AssertException("Unexpected exception thrown"); 18: } 19:   20: file.Readable = false; 21:   22: // Should NOT be able to read file. 23: try 24: { 25: file.Read(); 26: throw new AssertException("No exception thrown"); 27: } 28: catch (AccessViolationException) 29: { 30: } 31: catch (Exception) 32: { 33: throw new AssertException("Unexpected exception thrown"); 34: } 35:   36: // Clean up 37: file.Delete(); 38: } 39: }

    Now I've added a handler for unexpected exceptions but this is still a pretty cumbersome way to write tests verifying that exceptions are thrown or not correctly. Especially since Xunit.net provides us with a really simple way to handle this for us. That is what will happen next.

  • Being Cellfish

    2008 Advent Calendar December 1st

    • 1 Comments
    
    
    1: public class Advent1 2: { 3: [Fact] 4: public void TestRead() 5: { 6: // Create a test file 7: IFileUtil file = new FileUtil("SomeFile.txt"); 8: file.Create("CONTENT"); 9:   10: // Should be able to read file 11: try 12: { 13: file.Read(); 14: } 15: catch (Exception) 16: { 17: throw new AssertException("Unexpected exception thrown"); 18: } 19:   20: file.Readable = false; 21:   22: // Should NOT be able to read file. 23: try 24: { 25: file.Read(); 26: throw new AssertException("No exception thrown"); 27: } 28: catch (AccessViolationException) 29: { 30: } 31:   32: // Clean up 33: file.Delete(); 34: } 35: }

    With this first version of the test there I have not utilized all the features of Xunit.net. I basically do all the verification my self and create test failures by explicitly throwing AssertFailure exceptions. There are several problems with this code but the only problem that will be addressed in the next version of this test is if line 25 throws another exception than AccessViolationException. Xunit.net will save the day but it is quite ugly So in tomorrows version we'll fix that.

Page 3 of 3 (25 items) 123