Как я могу получить доступ к Волшебству в моих тестах RSpec?
Жемчужина аутентификации волшебства: https://github.com/NoamB/sorcery
Создатель Sorcery предоставляет пример приложения Rails с помощниками теста Sorcery, включенными в его функциональные тесты Test::Unit: https://github.com/NoamB/sorcery-example-app/blob/master/test/functional/users_controller_test.rb
# Test::Unit functional test example
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
setup do
@user = users(:noam)
end
test "should show user" do
login_user
get :show, :id => @user.to_param
assert_response :success
end
Но я не могу понять, как получить login_user
работать в моих спецификациях контроллера RSpec.
/gems/sorcery-0.7.5/lib/sorcery/test_helpers/rails.rb:7:in `login_user':
undefined method `auto_login' for nil:NilClass (NoMethodError)
Вот соответствующий код в драгоценном камне Волшебства относительно вышеупомянутой ошибки: https://github.com/NoamB/sorcery/blob/master/lib/sorcery/test_helpers/rails.rb
module Sorcery
module TestHelpers
module Rails
# logins a user and calls all callbacks
def login_user(user = nil)
user ||= @user
@controller.send(:auto_login,user)
@controller.send(:after_login!,user,[user.send(user.sorcery_config.username_attribute_names.first),'secret'])
end
def logout_user
@controller.send(:logout)
end
end
end
end
ОБНОВИТЬ:
В соответствии с документацией Колдовства " Тестирование в Rails 3" я действительно добавил include Sorcery::TestHelpers::Rails
к моему spec_helper.rb
,
Помощник теста Волшебства login_user
действует на @controller
но я получаю ошибку, потому что @controller
является nil
в моем контроллере спец. Вот моя спецификация:
#spec/controllers/forums_controller_spec.rb
require 'spec_helper'
describe ForumsController do
render_views
describe 'GET new' do
describe 'when guest' do
it 'should deny and redirect' do
get :new
response.should redirect_to(root_path)
end
end
describe 'when admin' do
p @controller #=> nil
@user = User.create!(username: "Test", password: "secret", email: "test@test.com")
login_user # <--------------- where the error occurs
it 'should resolve' do
get :new
response.should render_template(:new)
end
end
end
end
6 ответов
FWIW, я потратил много времени в поисках ответа на эту проблему. Я использую Capybara и RSpec. Оказывается, вам нужно войти в систему вручную с помощью Sorcery, чтобы получить логин для работы.
Я создал Gist для создания интеграционных тестов с помощью Sorcery/Rspec/Capybara здесь: https://gist.github.com/2359120/9989c14af19a48ba726240d030c414b882b96a8a
Вы должны включить помощников теста Волшебства в свой spec_helper
include Sorcery::TestHelpers::Rails
Посмотрите волшебную вики: https://github.com/NoamB/sorcery/wiki/Testing-rails-3
В примере приложения rails это делается по адресу https://github.com/NoamB/sorcery-example-app/blob/master/test/test_helper.rb#L13
обновленный
У вас есть другие спецификации контроллера в той же папке, которые успешно проходят? RSpec обычно смешивает необходимые компоненты для тестирования контроллера для спецификаций в папке "spec/controllers".
Вы можете попробовать явно пометить это как спецификацию контроллера, написав
describe ForumsController, :type => :controller do
Вам нужно поместить свое пользовательское создание и войти в блок before(:each) следующим образом:
describe 'when admin' do
before(:each) do
@user = User.create!(username: "Test", password: "secret", email: "test@test.com")
login_user
end
it 'should resolve' do
get :new
response.should render_template(:new)
end
end
Для нового приложения в Rails 7.0.0 я исправил эту проблему, добавив:
# spec/rails_helper.rb
config.include ::Sorcery::TestHelpers::Rails::Controller
и тогда я мог бы использовать метод из Sorcery, не определяя свой собственный:
# spec/controllers/registrations_controller_spec.rb
require 'rails_helper'
RSpec.describe RegistrationsController, type: :controller do
let(:current_user) { nil }
before do
login_user(current_user) if current_user
end
describe "when user is logged in" do
let(:current_user) { create(:user) }
it "action new redirects to root" do
get :new
expect(subject).to redirect_to(root_path)
expect(flash[:notice]).to eq(I18n.t("registration.new.already_logged_in"))
end
end
end
Добавление только::Sorcery::TestHelpers::Rails не смог найти метод login_user.
Надеюсь это поможет.
Я только что испытал эту дилемму сам и опираясь на данные от danneu, diwalak и Birdlevitator (в этом тесте контроллера thead: rail3 / rspec / devise: rspec не удается, если я не добавлю dummy = subject.current_user.inspect), я думаю, что смогу увидеть решение.
Я работал со стандартным ресурсом rails 3 rspec, созданным командой 'rails generate scaffold'. Вот файл rspec контроллера после того, как я изменил его для работы с логином магии:
require 'spec_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
describe RecordsController do
before(:each) do
@user = User.create!(forename: "Billy", surname: "Bob", username: "Test", password: "secret!1", email: "test@test.com")
login_user
end
# This should return the minimal set of attributes required to create a valid
# Record. As you add validations to Record, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ :owner => 'Mr Blobby', :catagory => 'Index'}
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# RecordsController. Be sure to keep this updated too.
def valid_session
{"warden.user.user.key" => session["warden.user.user.key"]}
end
describe "GET index" do
it "assigns all records as @records" do
record = Record.create! valid_attributes
get :index, {}, valid_session
assigns(:records).should eq([record])
end
end
describe "GET show" do
it "assigns the requested record as @record" do
record = Record.create! valid_attributes
get :show, {:id => record.to_param}, valid_session
assigns(:record).should eq(record)
end
end
describe "GET new" do
it "assigns a new record as @record" do
get :new, {}, valid_session
assigns(:record).should be_a_new(Record)
end
end
describe "GET edit" do
it "assigns the requested record as @record" do
record = Record.create! valid_attributes
get :edit, {:id => record.to_param}, valid_session
assigns(:record).should eq(record)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Record" do
expect {
post :create, {:record => valid_attributes}, valid_session
}.to change(Record, :count).by(1)
end
it "assigns a newly created record as @record" do
post :create, {:record => valid_attributes}, valid_session
assigns(:record).should be_a(Record)
assigns(:record).should be_persisted
end
it "redirects to the created record" do
post :create, {:record => valid_attributes}, valid_session
response.should redirect_to(Record.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved record as @record" do
# Trigger the behavior that occurs when invalid params are submitted
Record.any_instance.stub(:save).and_return(false)
post :create, {:record => {}}, valid_session
assigns(:record).should be_a_new(Record)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Record.any_instance.stub(:save).and_return(false)
post :create, {:record => {}}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested record" do
record = Record.create! valid_attributes
# Assuming there are no other records in the database, this
# specifies that the Record created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Record.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
put :update, {:id => record.to_param, :record => {'these' => 'params'}}, valid_session
end
it "assigns the requested record as @record" do
record = Record.create! valid_attributes
put :update, {:id => record.to_param, :record => valid_attributes}, valid_session
assigns(:record).should eq(record)
end
it "redirects to the record" do
record = Record.create! valid_attributes
put :update, {:id => record.to_param, :record => valid_attributes}, valid_session
response.should redirect_to(record)
end
end
describe "with invalid params" do
it "assigns the record as @record" do
record = Record.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Record.any_instance.stub(:save).and_return(false)
put :update, {:id => record.to_param, :record => {}}, valid_session
assigns(:record).should eq(record)
end
it "re-renders the 'edit' template" do
record = Record.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Record.any_instance.stub(:save).and_return(false)
put :update, {:id => record.to_param, :record => {}}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested record" do
record = Record.create! valid_attributes
expect {
delete :destroy, {:id => record.to_param}, valid_session
}.to change(Record, :count).by(-1)
end
it "redirects to the records list" do
record = Record.create! valid_attributes
delete :destroy, {:id => record.to_param}, valid_session
response.should redirect_to(records_url)
end
end
end
И множество важных битов:
Этот бит выполняет программный вход в систему (игнорируя атрибуты "имя" и "фамилия", они специфичны для решения, которое я создаю):
before(:each) do
@user = User.create!(forename: "Billy", surname: "Bob", username: "Test", password: "secret!1", email: "test@test.com")
login_user
end
Этот бит содержит информацию о сеансе / данные ключа:
def valid_session
{"warden.user.user.key" => session["warden.user.user.key"]}
end
Как пишет diwalak, нам нужно добавить это в файл spec_help.rb:
include Sorcery::TestHelpers::Rails
И это все - у меня все равно получилось:)
Закончилось переключение на Devise, который гораздо лучше задокументирован и имеет больший кругозор для любых вопросов, которые у меня могут возникнуть.