Sterling Rose Design Blog

assert_include Workaround

7 Comments
Tags: testing Rails

I’m not sure if there used to be an assert_include method or not (I’ve seen some references to it, but can’t find it anywhere in the Rails API), but I found a workaround.

I have a Color model, and I want the :index action to only show those colors whose deleted value is set to false (it’s more or less what the acts_as_paranoid plugin did/does). My test for the :index action looks like this:

test "should get only colors that are not deleted" do
  first_color = Color.create(:name => 'Blue', :deleted => false)
  second_color = Color.create(:name => 'Red', :deleted => true)
  get :index

  assert_response :success
  assert_not_nil assigns(:colors)
  assert_equal true, assigns(:colors).include?(first_color)
  assert_equal false, assigns(:colors).include?(second_color)
end


There may be other ways to do this (and I certainly wel...


Copyright 2007-2010, Sterling Rose Design. All rights reserved.