Warp
Warp provides a selection of inteligent RSpec matchers for your model, controller and feature specifications.
Warp's matchers are written to be intuitive, easy to use, and to avoid testing Rails itself. The matchers also take advantage of RSpec's composing matchers wherever possible.
Compatible with:
- Ruby 1.9.3 and greater
- Rails 3.2 and greater
- RSpec 3.0.0 and greater
Installation
Add this line to your application's Gemfile:
gem "warp"
And then execute:
$ bundle
Usage
Assign Matchers
assigns(key)
Ensures that the specific assign is set:
specify { expect(controller).to assign(:posts) }
assigns(key).with(object)
Ensures that the specific assign is set with the specified value:
specify { expect(controller).to assign(:posts).with(posts) }
assigns(key).with_a(klass)
Ensures that the specific assign is set with an instance of the specified class:
specify { expect(controller).to assign(:post).with_a(Post) }
assigns(key).with_a_new(klass)
Ensures that the specific assign is set with a instance of the specified class that is not persisted:
specify { expect(controller).to assign(:post).with_a_new(Post) }
Flash Matchers
set_flash(key)
Ensure that the specific flash key is set:
specify { expect(controller).to set_flash(:notice) }
set_flash(key).to(value)
Ensure that the specific flash key is set:
specify { expect(controller).to set_flash(:notice).to("Your order has been processed.") }
Association Matchers
belong_to(association)
Ensures that a belong_to
association is present:
specify { expect(comment).to belong_to(:post) }
Works with either model classes or model objects.
have_many(association)
Ensures that a has_many
association is present:
specify { expect(post).to have_many(:comments) }
have_one(association)
Ensures that a has_one
association is present:
specify { expect(person).to have_one(:address) }
have_and_belong_to_many(association)
Ensures that a has_and_belongs_to_many
association is present:
specify { expect(group).to have_and_belong_to_many(:users) }
This matcher is not avaliable on Rails 4.1+, as these versions of Rails will create a has_many :though
association instead of a 'has_and_belongs_to_many' association.
Attribute Matchers
have_attribute(attribute)
Checks if a certain attribute is present on a model. Works against models or model instances.
specify { expect(user).to have_attribute(:name) }
Validation Matchers
All validation matchers can be specified with, or without options to verify.
validate_acceptance_of
Ensures that a validates_acceptance_of
validator is present on the attribute.
specify { expect(user).to validate_acceptance_of(:terms) }
validate_absence_of
Ensures that a validates_absence_of
validator is present on the attribute.
Only avaliable on Rails 4+.
specify { expect(user).to validate_absence_of(:postcode, if: :non_us_address) }
validate_confirmation_of
Ensures that a validates_confirmation_of
validator is present on the attribute.
specify { expect(user).to validate_confirmation_of(:password) }
validate_exclusion_of
Ensures that a validates_exclusion_of
validator is present on the attribute.
specify { expect(user).to validate_exclusion_of(:age, in: 0..17) }
validate_format_of
Ensures that a validates_format_of
validator is present on the attribute.
specify { expect(user).to validate_format_of(:postcode, with: /\d{5,8}/) }
validate_inclusion_of
Ensures that a validates_inclusion_of
validator is present on the attribute.
specify { expect(user).to validate_inclusion_of(:role, in: ["develop", "design"]) }
validate_length_of
Ensures that a validates_length_of
validator is present on the attribute.
specify { expect(user).to validate_length_of(:name, in: 5..25) }
validate_numericality_of
Ensures that a validates_numericality_of
validator is present on the attribute.
specify { expect(user).to validate_numericality_of(:age) }
validate_presence_of
Ensures that a validates_presence_of
validator is present on the attribute.
specify { expect(user).to validate_presence_of(:name) }
validate_association_of
Ensures that a validates_association_of
validator is present on the attribute.
specify { expect(user).to validate_association_of(:profile) }
validate_uniqueness_of
Ensures that a validates_uniqueness_of
validator is present on the attribute.
specify { expect(user).to validate_uniqueness_of(:email, scope: :company) }
Action Matchers
create
Ensures that a record is created.
specify { expect{post :create, params}.to create(Post) }
update
Ensures that a record is updated.
specify { expect{post :update, params}.to update(Post) }
destroy
Ensures that a record is destroyed.
specify { expect{delete :destroy, id: id}.to destroy(Post) }
Contributing
- Fork it
- Create a branch (
git checkout -b super-foo
) - Add your feature and specs.
- Commit your changes (
git commit -am 'Extra-super foo-matic.'
) - Push to the branch (
git push origin super-foo
) - Create new Pull Request