Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rspecのテスト環境でdeviseにログインする方法【rails】 ref: http://qiita.com/Kohei_Kishimoto0214/items/e29e509b12a6eb484a42
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
gem 'devise'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'faker'
gem 'rails-controller-testing'
$ gem install rails-controller-testing
$ gem list con
=> rails-controller-testing (1.0.1)
# 記述なし
FactoryGirl.create(:user)
#記述あり
create(:user)
# 記述なし
FactoryGirl.create(:user)
#記述あり
create(:user)
def index
@groups = current_user.groups
end
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
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
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
You can’t perform that action at this time.