February 5, 2009

Machinist and Rails controller tests

Machinist, my test-data generation plugin for Rails, has just gained some features that make it useful for controller tests. You can now do things like:

test "should create post" do
  assert_difference('Post.count') do
    post :create, :post => Post.plan
  end
  assert_redirected_to post_path(assigns(:post))
end

You an also call plan on ActiveRecord associations, making it easy to test nested controllers:

test "should create comment" do
  post = Post.make
  assert_difference('Comment.count') do
    post :create, :post_id => post.id, :comment => post.comments.plan
  end
  assert_redirected_to post_comment_path(post, assigns(:comment))
end

Pete, this is really awesome. I’ll be putting this to use this week :)

Ben Schwarz
February 5, 2009

Very nice. And http://github.com/notahat/machinist/commit/42e828e832c40175fa7c590784c3564406f1d3d2 floats my boat as well!

February 5, 2009
  1. Use Markdown for formatting.

  2. I’ll keep your email address private.