Ruby on rails 迈克尔·哈特尔';s的RubyonRails教程。第9章中的测试失败

Ruby on rails 迈克尔·哈特尔';s的RubyonRails教程。第9章中的测试失败,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我是一个新手,正在学习Michael Hartl的Tuby on Rails教程,在第9章的测试中,我有几个失败的项目 运行RSPEC测试返回: sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/ ...................................FF................................ Failures: 1) Authentication authorization in

我是一个新手,正在学习Michael Hartl的Tuby on Rails教程,在第9章的测试中,我有几个失败的项目

运行RSPEC测试返回:

sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/
...................................FF................................

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_2::Nested_3::Nested_2::Nested_1:0x007fca6433d3b8>
 # ./spec/requests/authentication_pages_spec.rb:72: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_2::Nested_3::Nested_2::Nested_2:0x007fca6434db50>
 # ./spec/requests/authentication_pages_spec.rb:77:in `block (5 levels) in <top (required)>'

Finished in 1.85 seconds
69 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:73 # Authentication authorization in the Users controller visiting the edit page 
rspec ./spec/requests/authentication_pages_spec.rb:78 # Authentication authorization in the Users controller submitting to the update action 
这是上面73行

it { should have_selector('title', text: 'Sign in') }
specify { response.should redirect_to(signin_path) }
从上面看第78行

it { should have_selector('title', text: 'Sign in') }
specify { response.should redirect_to(signin_path) }
有什么想法吗?我真的不明白这是什么意思。谢谢Si。

在您的第50行

let(:user) { FactoryGirl.create(:user) }
但是:user在您到达第73行时不再可用,因为您关闭了第68行中定义的描述块:user。第77行同样的内容,您尝试再次使用它

我的建议是将let(:user)移动到规范的顶部,这样您只需要定义一次,而不是将其包含在整个规范中。如果不这样做,请在第71行(在之后的行描述“在用户控制器中”do


潜在的解决方案是定义let(:user)。。。。。在顶部,因此您只需要定义一次用户,而不是在每个块中

require 'spec_helper'

describe "authentication" do
  subject { page }
  let(:user) { FactoryGirl.create(:user) }
  ...

好的,你能解释一下“顶级规格”在哪里或什么地方吗?对不起,我是新手!西蒙尼理解了第71行。重新定义了它,但同样的问题仍然存在。仍然不明白“顶级规范”是什么意思。请尝试重新启动rails服务器。这也可能有助于您将其置于上下文中:谢谢Jeremy,但我真的不知道它与Rails服务器有什么关系——而且我不明白您试图用代码帮助我理解的上下文是什么。对不起…嘿,好了!谢谢,杰里米。最高规格!我会记住的,谢谢!