Created
April 15, 2013 10:35
-
-
Save ekampp/5387223 to your computer and use it in GitHub Desktop.
Some shared context and examples that I often use when dealing with CRUD actions in Rspec.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shared_examples :denied_access do | |
before{ action } | |
it { request.should redirect_to sign_in_path } | |
it { response.status.should eq 302 } | |
end | |
shared_examples :response_ok do | |
before{ action } | |
it { response.should be_success } | |
it { response.status.should eq 200 } | |
end | |
shared_examples :existing_resource do | |
include_context :resource_class | |
it "should expose the right resource" do | |
action | |
controller.send(resource_name).should eq resource | |
end | |
end | |
shared_examples :found_and_redirected do | |
before{ action } | |
it { response.status.should eq 302 } | |
it { response.should be_redirect } | |
it "should redirect to right location" do | |
if defined?(location) | |
response.location.should match location | |
end | |
end | |
end | |
shared_examples :granted_access do | |
it "should not redirect to the sign in path" do | |
action | |
response.should_not redirect_to sign_in_path | |
end | |
end | |
shared_context :resource_class do | |
before do | |
@resource_class = resource_name.to_s.classify.constantize | |
end | |
end | |
shared_examples :destroyed_resource do | |
include_context :resource_class | |
it "should destroy the resource" do | |
action | |
@resource_class.count.should eq 0 | |
end | |
end | |
shared_examples :created_resource do | |
include_context :resource_class | |
it "should create the resource" do | |
expect{ action }.to change{@resource_class.count}.by(1) | |
end | |
end | |
shared_examples :not_created_resource do | |
include_context :resource_class | |
it "should not create the resource" do | |
previous_count = @resource_class.count | |
action | |
@resource_class.count.should eq previous_count | |
end | |
end | |
shared_examples :malformatted_params do | |
it "should raise an error" do | |
expect{ action }.to raise_error(ActionController::ParameterMissing) | |
end | |
end | |
shared_examples :new_resource do | |
include_context :resource_class | |
before{ action } | |
describe "exposed resource" do | |
subject{ controller.send(@resource_class.to_s.underscore) } | |
its(:new_record?) { should be_true } | |
its(:persisted?) { should be_false } | |
it { should be_a @resource_class } | |
end | |
end | |
shared_examples :not_updated_resource do | |
include_context :resource_class | |
it "should change the values" do | |
action | |
atrs.each do |key, value| | |
resource.reload.send(key).to_s.should_not eq value.to_s | |
end | |
end | |
end | |
shared_examples :updated_resource do | |
include_context :resource_class | |
it "should change the values" do | |
action | |
atrs.each do |key, value| | |
resource.reload.send(key).to_s.should eq value.to_s | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment