Created
March 18, 2015 11:59
-
-
Save kouyaf77/069477939a580ff0ae2b to your computer and use it in GitHub Desktop.
Rspec test
This file contains 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
require "rails_helper" | |
describe TestsController, type: :controller do | |
render_views | |
let(:tests){FactoryGirl.create_list :test, 3} | |
let(:test){FactoryGirl.create :test} | |
describe "GET #index" do | |
before {get :index} | |
it {expect(assigns :tests).to be_an ActiveRecord::Relation} | |
it {expect(assigns :tests).to eq tests} | |
it {expect(response).to render_template :index} | |
end | |
describe "POST #create" do | |
let(:test_attributes) do | |
test.attributes.except "id", "created_at", "updated_at" | |
end | |
before do | |
expect_any_instance_of(Test).to receive(:create) | |
.and_return result_of_save | |
post :create, test: test_attributes | |
end | |
context "when saving succeeded" do | |
before {let(:result_of_save){true}} | |
it {expect(response).to redirect_to :tests] | |
end | |
context "when saving failed" do | |
before {let(:result_of_save){false}} | |
it {expect(assigns :test).to eq Test.new(test_attributes)} | |
it {expect(response).to render_template :new} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment