helper for rspec
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rvmrc
Changelog
Gemfile
MIT-LICENSE
README.rdoc
Rakefile
spec_support.gemspec

README.rdoc

SpecSupport

Depends on ActiveModel (and implictly on ActiveRecord for the check_all_columns functioality).

check_all_columns

After building a factory that fills out all the attributes (columns) of an ActiveRecord model, I want to make sure all attributes/columns actually have data.

This aids in avoiding the gotcha where a belongs_to has a name that is not exactly matching with the _id column, which goes undetected in many naive tests (a simple build on the belongs_to will pass, a save will pass, a reload and reading back the belongs_to will pass and indicate correct values (because reload does not touch values that are not actually columns). Reading all the _id columns is one way to detect this issue.

Example usage:

describe Contact do

  it "has all columns for a full factory" do
    contact = FactoryGirl.create(:full_contact)
    contact = Contact.find(contact.id)
    check_all_columns(contact)
  end

end

delete_nil_values

In default Rspec controller specs, default values are required. A nice way to produce them automatically from the FactoryGirl factories is:

def valid_attributes
  FactoryGirl.build(:full_contact).attributes.delete_nil_values
end

The reason the nil values need to be removed is to allow some default key (e.g. created_at etc.) to be set by ActiveRecord when executing the create in the controller. Specifically, without the delete_nil_values, this error results:

null value in column "created_at" violates not-null constraint

present_attributes

SpecSupport.present_attributes(m)

is a combined function that executes

m.attributes.delete_nil_values

shown above as valid_attributes on a ActiveRecord model (typically produced with FactoryGirl)

has_query_params?

in a spec do:

class String
  include SpecSupport::QueryParams
end

URI.unescape(rendered).should have_query_params(
  "f[key][]", "value+with+spaces")

has_error_key?

in a spec do:

subject.errors.should have_error_key(:name, :blank)

to validate that the “name” attribute violates the presence validation (and has exactly that error).