[TestMethod] [ExpectedException(typeof(SomeException))] public void CanDoSomething_WithSomeThingThatThrows_ReturnsTrue() { var something = SomeFactory.GetSomething(); something.DoSomethingI was tired, it was late, I'm used to a using different test framework but I got in contact straight away.(x => { throw new NotImplementedException(); }); var result = something.DoSomethingElse<ISomeThingElse>(); Assert.IsTrue(true); }
....spotted something in your codez. You're Assert is true everytime.Of course dear reader you will have spotted the ExpectedException attribute as you, unlike me are not an idiot but my eyes see:
[TEST BLAH] [yes i didn't spot the attrib here] public void STUFF_I_WILL_READ_IF_TEST_IS_NOT_OBVIOUS() { //SOME ARRANGING STUFF ..blah //THIS IS IMPORTANT var result = something.DoSomethingElse<ISomeThingElse>(); //AND SO IS THIS Assert.IsTrue(true); }In my semi-comatosed state my eyes fix on Assert.IsTrue(true) and I can look away from it. I'm used to NUnit's (and xUnit and mbUnit) Assert.Throws
[TEST BLAH] public void STUFF_I_WILL_READ_IF_TEST_IS_NOT_OBVIOUS() { //SOME ARRANGING STUFF ..blah //THIS IS IMPORTANT Assert.ThrowsHowever some frameworks don't support this. So you've got 2 options.(()=> something.DoSomethingElse<ISomeThingElse>()); }
Option 1 Attribute and fail.
[TestMethod] [ExpectedException(typeof(SomeException))] public void CanDoSomething_WithSomeThingThatThrows_ReturnsTrue() { var something = SomeFactory.GetSomething(); something.DoSomethingAs my eyes always look the assert and so if I see a fail. I'll look to see why.(x => { throw new NotImplementedException(); }); var result = something.DoSomethingElse<ISomeThingElse>(); Assert.Fail(); }
Option 2 No Attribute try catch
[TestMethod] public void CanDoSomething_WithSomeThingThatThrows_ReturnsTrue() { var something = SomeFactory.GetSomething(); something.DoSomethingAnd what happens when we want to invoke an action that doesn't expect anything. That you just want to run to see if doesn't fail. Well you've got Assert.DoesNotThrow in most .Net unit testing frameworks.(x => { throw new NotImplementedException(); }); try { var result = something.DoSomethingElse<ISomeThingElse>(); Assert.Fail(); } catch (SomeException) { Assert.IsTrue(true); } catch (Exception) { Assert.Fail(); } }
You can call the Assert.IsTrue(True);
[TestMethod] public void CanDoSomething_WithSomeThingThatDoesnotThrow() { var something = SomeFactory.GetSomething(); something.DoSomethingOr just drop the assert.(x => { throw new NotImplementedException(); }); Assert.IsTrue(True); }
[TestMethod] public void CanDoSomething_WithSomeThingThatDoesnotThrow() { var something = SomeFactory.GetSomething(); something.DoSomethingSo with MStest you can't use Assert.Throws but you do have options. Mine is to use another framework ;)(x => { throw new NotImplementedException(); }); }
1 comment:
Before I discovered the NUnit Assert.Throws syntax I used to use ExpectedException and have an Assert.IsTrue(false) at the end of the test. But I always put a message in there to explain why it was fail.
It never felt right to just not have an assert but at the same time that didn't look quite right either.
One problem I have with testing constructors throw via Assert.Throws is that you have to have curly brackets in your lambda EG
Assert.Throws(() => { var myClass = new myClass(null)} );
It just doesn't sit right with me
Post a Comment