我在使用rspec和设计测试用户身份验证时遇到了问题。
这是我的代码:
require 'rails_helper'
describe UsersController, :type => :controller do
let(:user) { User.create!(email: '1@test.com', password: '123456') }
describe 'GET #show' do
context 'User is logged in' do
before
sign_in user
get :show
end
it "loads correct user details" do
expect(assigns(:user)).to eq user
end
end
context 'No user is logged in' do
it 'redirects to login' do
get :show, id: user.id
expect(response).to redirect_to(root_path)
end
end
end
end
当我在终端上运行rspec时,我收到此错误:
/Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load': /Users/joao1/Desktop/nameofapp/spec/controllers/users_controller_spec.rb:29: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/joao1/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/joao1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
我如何解决这个问题并有效地测试我的用户身份验证?
答案 0 :(得分:1)
错误消息是程序中输入错误的结果,并且它与端块没有匹配。仔细观察我发现你错过了前一块的do语句。将其更改为:
before do
sign_in user
get :show
end