Last active
June 26, 2017 09:27
-
-
Save Koh0214/d375c8e3dfb2d8243c9f0808fbd728d3 to your computer and use it in GitHub Desktop.
rspecのテスト環境でdeviseにログインする方法【rails】 ref: http://qiita.com/Kohei_Kishimoto0214/items/e29e509b12a6eb484a42
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
module ControllerMacros | |
def login_admin(admin) | |
@request.env["devise.mapping"] = Devise.mappings[:admin] | |
sign_in admin | |
end | |
def login_user(user) | |
controller.stub(:authenticate_user!).and_return true | |
@request.env["devise.mapping"] = Devise.mappings[:user] | |
sign_in user | |
end | |
end |
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
gem 'devise' | |
gem 'rspec-rails' | |
gem 'factory_girl_rails' | |
gem 'faker' | |
gem 'rails-controller-testing' |
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
$ gem install rails-controller-testing |
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
$ gem list con | |
=> rails-controller-testing (1.0.1) |
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
# 記述なし | |
FactoryGirl.create(:user) | |
#記述あり | |
create(:user) |
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
# 記述なし | |
FactoryGirl.create(:user) | |
#記述あり | |
create(:user) |
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
def index | |
@groups = current_user.groups | |
end |
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 MessagesController, type: :controller do | |
let(:user) { create(:user) } | |
# userをcreateし、let内に格納 | |
describe 'GET #index' do | |
before do | |
login_user user | |
# controller_macros.rb内のlogin_userメソッドを呼び出し | |
end | |
it "renders the :index template" do | |
end | |
end |
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
RSpec.configure do |config| | |
config.include FactoryGirl::Syntax::Methods | |
end |
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
FactoryGirl.define do | |
pass = Faker::Internet.password(8) | |
factory :user do | |
name Faker::Name.name | |
email Faker::Internet.email | |
password pass | |
password_confirmation pass | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment