0.0
No commit activity in last 3 years
No release in over 3 years
Add useful spec/support functionalities that come back in each project
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

 Project Readme

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).