Test Your respond_to Block

Getting RESTful and wondering how to test your respond_to block in your controller to make sure its returning the right format? Drop these into your test_helper.rb.

def assert_xml
  assert_match 'application/xml', @response.headers['Content-Type']
end
 
def assert_rss
  assert_match 'application/rss+xml', @response.headers['Content-Type']
end

Then in your functional tests, use them like this:

def test_return_xml
  get :index, :format => 'xml'
  assert_xml
end
 
def test_return_rss
  get :index, :format => 'rss'
  assert_rss
end

Yay!

Popularity: 5% [?]

    Categories: Rails, Ruby, Snippets     2 Comments »

Hold the Associates, Please

Want to populate a select box with just the items you HAVEN'T associated?

@post = Post.find(params[id])
@categories = Category.find(:all) - @post.categories

Popularity: 4% [?]

    Categories: Ruby, Snippets     No Comments »