Ruby on rails Rails教程9.3更新用户身份验证失败的测试
我对Rails教程(Rails 3)有问题。我目前正在进行9.28 rSpec测试,我一直在不断地寻找我犯了这两个错误的原因Ruby on rails Rails教程9.3更新用户身份验证失败的测试,ruby-on-rails,ruby,ruby-on-rails-3,rspec,railstutorial.org,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,Railstutorial.org,我对Rails教程(Rails 3)有问题。我目前正在进行9.28 rSpec测试,我一直在不断地寻找我犯了这两个错误的原因 user@ubuntu:~/rails_projects/sample_app$ bundle exec rspec spec/ ......................F.F............................................ Failures: 1) Authentication authorization in t
user@ubuntu:~/rails_projects/sample_app$ bundle exec rspec spec/
......................F.F............................................
Failures:
1) Authentication authorization in the Users controller visiting the edit page
Failure/Error: before { visit edit_user_path(user) }
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3::Nested_2::Nested_1:0x9685354>
# ./spec/requests/authentication_pages_spec.rb:75:in `block (5 levels) in <top (required)>'
2) Authentication authorization in the Users controller submitting to the update action
Failure/Error: before { put user_path(user) }
NameError:
undefined local variable or method `user' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3::Nested_2::Nested_3:0x9b12034>
# ./spec/requests/authentication_pages_spec.rb:85:in `block (5 levels) in <top (required)>'
Finished in 10.86 seconds
69 examples, 2 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:76 # Authentication authorization in the Users controller visiting the edit page
rspec ./spec/requests/authentication_pages_spec.rb:86 # Authentication authorization in the Users controller submitting to the update action
真的,我已经花了很长一段时间试图弄明白这一点,如果有可能通过解释(如果不是打字错误的话)将是非常棒的。开始掌握Rails,但还没有完全掌握。如果重新输入凌乱的规范代码,您会发现
描述“在用户控制器中”
块中缺少用户定义
鉴于您在授权中的所有三个descripe
块中都使用了user
,我将其上移一级,这样您将得到:
describe "authorization" do
let(:user) { FactoryGirl.create(:user) }
describe "for non-signed-in users" do
...
end
describe "in the Users controller" do
...
end
describe "as wrong user" do
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
...
end
end
如果你的缩进正确,它会更清晰(对你和那些想要帮助你的人来说)。每个缩进使用两个空格,并使用StackOverflow中的
{}
按钮对整个代码块进行代码引用。我试图修复缩进的代码以进行授权,感谢您的建议。谢谢您的mechanicalfish。在清理了行中的代码和stackoverflow上的代码之后,我有一个结尾,它结束了for_non-signed-in_users块,因此没有在代码中声明user After。我也做了你的修复,将用户上移到类/嵌套/层(这就是它的名称吗?),效果也一样。非常感谢。如果有人能认可他的答案,那就太好了。人们叫它descripe block,不知道它是否有更好的名字,因为我不太经常使用rspec。你也应该能够接受答案。
describe "authorization" do
let(:user) { FactoryGirl.create(:user) }
describe "for non-signed-in users" do
...
end
describe "in the Users controller" do
...
end
describe "as wrong user" do
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
...
end
end