﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Heiko Hatzfeld commented on What is one assert?</title><description>Damn no Edit... I forgot something...
  
  
The thing you are loosing with multiple asserts is a good defect location. The Example with Mocks from Gian Maria is different. Here the feedmack gained from the test will include all failures (I admit that i am using NMock... So dont hit me to hard pls) So I can see from a glance that the mock was not correctly excersiced. 
  
  
The Failure will give me a list of ALL expectation that where violated, and not stop at the 1st one. Thats a crutial difference in the amount of feedback that I get from a single test!
</description><link>http://ayende.com/2684/what-is-one-assert#comment10</link><guid>http://ayende.com/2684/what-is-one-assert#comment10</guid><pubDate>Wed, 29 Aug 2007 22:18:25 GMT</pubDate></item><item><title>Heiko Hatzfeld commented on What is one assert?</title><description>Hello...
  
  
I just recently added your Blog to my reader, so I have to comment this a little bit later then the rest of the Folks here...
  
  
I must disagree with all those that agree... ;)
  
  
I think you are loosing one important feature of TDD if you have more then one Assert per test! In NUnit an Assert will throw an exception when it is invalid. That means if i have made a change in my code, that managed to break more then one of the asserts in my test, then I will have to do more then one itteration to "find" all bugs
  
  
Example: I managed to break 3 fields in a class after a db shema change... If I have only one assert for one testcase, then I would see EXACTLY that I have 3 fields that I need to fix.
  
  
If I have 1 testmethod that has 15 Asserts to validate all the fields, then I would have to fix the -ONE- reported error, only to get one more. If I dont "notice" that the Assert exception Message changed from 
  
  
"Expected that something should be this but i got nada"
  
  
to
  
  
"Expected that something should be that but i got nada"
  
  
Then I might think the fix which I just did -successfully- was invalid. Ill roll it back and try something else...
  
  
With one assert per test, I will see after the 1st Fix that I actually made progress! It will only report -2- more errors instead of -3- and I am feeling emotionaly better, since i made progress ;)
  
  
  
Also, for a simple mapping test like that, I would use an Implicit setup/teardown so all my testmethods would ONLY contain one assert, since all will run against the same object under test. 
  
  
Example:
  
  
[Test]
  
public void ShouldContainExactly3Rows()
  
{
  
Assert.AreEqual(3, rows.Count);
  
}
  
  
[Test]
  
public void FirstRowShouldBeAlFki()
  
{
  
Assert.AreEqual("ALFKI", rows[0]["Id"]);
  
}
  
  
Also I would not validate all 6 Fields of the 3 rows, but only the IDs (Test for checking the mapping of the Row itself are in another test class, so I know they work)
  
  
So the Number of test in the example would go from 7 to 3-4 and I would still feel that I have a good coverage.
  
  
I hope this makes sense ;)
  
  
Heiko Hatzfeld
</description><link>http://ayende.com/2684/what-is-one-assert#comment9</link><guid>http://ayende.com/2684/what-is-one-assert#comment9</guid><pubDate>Wed, 29 Aug 2007 22:10:17 GMT</pubDate></item><item><title>Colin Jack commented on What is one assert?</title><description>I agree completely. 
  
  
As Anders suggests I find refactoring these sorts of assertions into a well named cutom assertion method is often a good idea.
</description><link>http://ayende.com/2684/what-is-one-assert#comment8</link><guid>http://ayende.com/2684/what-is-one-assert#comment8</guid><pubDate>Fri, 10 Aug 2007 15:15:29 GMT</pubDate></item><item><title>Gian Maria commented on What is one assert?</title><description>I agree, for me the term "Single Assert" means Test a single functionality of the code, if the functionality means write a lot of Assert instruction I have no problem.
  
  
Usually when a programmer does heavvy Mock Testing he write a lot of expectation for each mock object, since every expectation is an assert actually I'm doing more than one assert instruction per test, but for me is fine if it is needed to test a single scenario. 
  
  
The important things to do are write a meaningful string for each assert so the error is clear and is clear why the test is failing.
  
  
Alk.
</description><link>http://ayende.com/2684/what-is-one-assert#comment7</link><guid>http://ayende.com/2684/what-is-one-assert#comment7</guid><pubDate>Tue, 07 Aug 2007 15:18:30 GMT</pubDate></item><item><title>Luke Melia commented on What is one assert?</title><description>This is one of those questions that makes me appreciate the context-oriented approach of RSpec and friends. When IronRuby arrives, you might have something like:
  
  
&lt;pre&gt;
  
context "With Ayende's nonsensical data" do
  
  
  setup do
  
    @rows = get_data #some method to setup the rows    
  
  end
  
  
  specify "there are three rows" do
  
    @rows.count.should.equal 3
  
  end
  
  
  specify "the first row is ALFKI" do
  
    @rows[0]["Id"].should.equal "ALFKI"
  
    @rows[0]["Name"].should.equal "Wrong id"
  
  end
  
  
  specify "the second row is BUMP" do
  
    @rows[1]["Id"].should.equal "BUMP"
  
    @rows[1]["Name"].should.equal "Goose"
  
  end
  
  
  specify "the third row is BUMP" do
  
    @rows[2]["Id"].should.equal "Lump"
  
    @rows[2]["Name"].should.equal "Of Rock"
  
  end
  
  
end
  
&lt;/pre&gt;
  
  
You achieve many of the ideals the single assertion per test (localize failures, continue running assertions after one fails). I've tried this with MbUnit in C# and it works, but it's quite heavy syntactically (one TestFixture per context) and that obscures the related nature of the multiple contexts your class under test can exist in.
  
  
In looking forward to Ruby and .NET, one of the most appealing and interesting things to me is using Ruby as a language for my testing my C# code.
  
  
Cheers from NYC,
  
Luke
</description><link>http://ayende.com/2684/what-is-one-assert#comment6</link><guid>http://ayende.com/2684/what-is-one-assert#comment6</guid><pubDate>Sun, 05 Aug 2007 20:48:30 GMT</pubDate></item><item><title>Anders Nor&amp;#229;s commented on What is one assert?</title><description>I think it depends on the scenarios your making the assertion. If you do it more than once, it might be more descriptive to refactor to a expected result. Another option is to write a custom assertion method (this might be the best alternative).
  
Whether an expected result with one assertion or multiple assertions is better is more or less a matter of taste imho.
</description><link>http://ayende.com/2684/what-is-one-assert#comment5</link><guid>http://ayende.com/2684/what-is-one-assert#comment5</guid><pubDate>Sun, 05 Aug 2007 19:39:12 GMT</pubDate></item><item><title>Ayende Rahien commented on What is one assert?</title><description>How is this different? Just because it has a single assert?
  
Also, even it would work, I would say that this is less obvious
</description><link>http://ayende.com/2684/what-is-one-assert#comment4</link><guid>http://ayende.com/2684/what-is-one-assert#comment4</guid><pubDate>Sun, 05 Aug 2007 19:34:38 GMT</pubDate></item><item><title>Anders Nor&amp;#229;s commented on What is one assert?</title><description>Provided that you have a ctor that accepts key/value pairs on the Row class, I guess you could write the test like this:
  
  
Row[] expectedRows=new Row[]
  
                                     {
  
                                         new Row(new KeyValuePair("ALFKI","Wrong id")),
  
                                         new Row(new KeyValuePair("BUMP","Goose")),
  
                                         new Row(new KeyValuePair("LUMP","Of Rock")),                                
  
                                     }
  
Assert.AreEqual(expectedRows,rows);
</description><link>http://ayende.com/2684/what-is-one-assert#comment3</link><guid>http://ayende.com/2684/what-is-one-assert#comment3</guid><pubDate>Sun, 05 Aug 2007 19:17:43 GMT</pubDate></item><item><title>Derik Whittaker commented on What is one assert?</title><description>I agree, to me the one assert per test is really one assert group.  
  
  
I know a test smells if i see some logic, then asserts, followed by more code which in turn is followed by more asserts.
  
  
Exampl
  
Logic....
  
Assert something
  
  
More Logic....
  
More Asserts
  
...
  
...
  
...
  
  
Derik
</description><link>http://ayende.com/2684/what-is-one-assert#comment2</link><guid>http://ayende.com/2684/what-is-one-assert#comment2</guid><pubDate>Sun, 05 Aug 2007 18:27:42 GMT</pubDate></item><item><title>JH commented on What is one assert?</title><description>Agreed!!
</description><link>http://ayende.com/2684/what-is-one-assert#comment1</link><guid>http://ayende.com/2684/what-is-one-assert#comment1</guid><pubDate>Sun, 05 Aug 2007 18:24:18 GMT</pubDate></item></channel></rss>