I wrote a post today for the FiveRuns corporate blog entitled ‘Beyond Continuous Integration: Continuous Information‘. It’s a quick look at some of the tools and practices we use daily on the development team.
I wrote a post today for the FiveRuns corporate blog entitled ‘Beyond Continuous Integration: Continuous Information‘. It’s a quick look at some of the tools and practices we use daily on the development team.
Pop quiz: which of these test assertions will pass?
assert true
assert true, true
assert true, false
assert true, 'false'
assert 'true', false
assert 'true', 'true'
assert 'true', 'false'
You might be surprised to learn that every single one of these assertions pass successfully. From the documentation, you can see that assert() takes 2 arguments: a boolean and an optional message. In each of the above cases, you’re passing a true value as the first argument and then an (unintended) optional message.
What you probably meant to do is use assert_equal(). This method takes 3 arguments: 2 boolean values and an optional message. This is the one you need when you intend to compare 2 values against each other.
It took me a little while to determine how to best test my Merb controller logic. Hopefully, this post will save you the time. The following is an excerpt from an example test/spec test of mine. It’s a tiny bit verbose for the sake of clarity.
context "Posts Controller" do
setup do
Post.delete_all
end
specify "should list all posts" do
create_valid_posts 4
headers = {}
params = {:action => :index}
request = Merb::Test::FakeRequest.with("/posts", "GET")
response = StringIO.new
controller = Posts.build(request, headers, params, response)
controller.dispatch(:index)
posts = controller.instance_variable_get("@posts")
assert 4, posts.size
end
#######
private
#######
num_posts.times do |num|
Post.create(:title => "Blog Post ")
end
end
end
There are a couple of key things to note here: