Michael Hartl 的 Ruby on Rails 教程.第 9 章测试失败 [英] Michael Hartl's Ruby on Rails Tutorial. Failed test in Chapter 9

查看:46
本文介绍了Michael Hartl 的 Ruby on Rails 教程.第 9 章测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a newbie working through Michael Hartl's Tuby on Rails Tutorial and I have a couple of failed items in a test in Chapter 9.

运行 RSPEC 测试返回:

Running RSPEC test returns:

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 

我的 authentication_pages_spec.rb

My authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

subject { page }

describe "signin page" do
before { visit signin_path }

it { should have_selector('h1',    text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end

describe "signin" do
before { visit signin_path }

describe "with invalid information" do
  before { click_button "Sign in" }

  it { should have_selector('title', text: 'Sign in') }
  it { should have_selector('div.alert.alert-error', text: 'Invalid') }

  describe "after visiting another page" do
    before { click_link "Home" }
    it { should_not have_selector('div.alert.alert-error') }
  end
 end

 describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  it { should have_selector('title', text: user.name) }
  it { should have_link('Profile',     href: user_path(user)) }
  it { should have_link('Sign out',    href: signout_path) }
  it { should have_link('Settings',    href: edit_user_path(user)) }
  it { should have_link('Users',       href: users_path) }
  it { should_not have_link('Sign in', href: signin_path) }

  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
  end
 end
end

describe "authorization" do

describe "for non-signed-in users" do
  let(:user) { FactoryGirl.create(:user) }

  describe "when attempting to visit a protected page" do
    before do
      visit edit_user_path(user)
      fill_in "Email",    with: user.email
      fill_in "Password", with: user.password
      click_button "Sign in"
    end

    describe "after signing in" do

      it "should render the desired protected page" do
        page.should have_selector('title', text: 'Edit user')
      end
    end
  end
  end

   describe "in the Users controller" do

    describe "visiting the edit page" do
      before { visit edit_user_path(user) }
      it { should have_selector('title', text: 'Sign in') }
    end

    describe "submitting to the update action" do
      before { put user_path(user) }
      specify { response.should redirect_to(signin_path) }
    end
  end
  end

  describe "as wrong user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
  before { sign_in user }

  describe "visiting Users#edit page" do
    before { visit edit_user_path(wrong_user) }
    it { should_not have_selector('title', text: full_title('Edit user')) }
  end

  describe "submitting a PUT request to the Users#update action" do
    before { put user_path(wrong_user) }
    specify { response.should redirect_to(root_path) }
  end
  end
end

这是上面的第 73 行

Here's line 73 from above

it { should have_selector('title', text: 'Sign in') }

和上面的第 78 行

specify { response.should redirect_to(signin_path) }

有什么想法吗?我真的不明白这意味着什么.谢谢 Si.

Any ideas? I'm really stuck as to what this means.Thanks Si.

推荐答案

在线 50 你有

let(:user) { FactoryGirl.create(:user) }

但是,当您到达第 73 行时 :user 不再可用,因为您关闭了 :user 在第 68 行中定义的描述块.在第 77 行,您尝试再次使用它时也是如此.

But :user is no longer available by the time you reach line 73 because you close off the describe block that :user is defined within in line 68. Same thing on line 77 where you try to use it again.

我的建议是将 let(:user) 移到规范的顶部,这样您只需定义一次而不是在整个规范中都包含它.如果失败,请在第 71 行(描述在用户控制器中"执行之后的行)重新定义它

My recommendation is to move the let(:user) to the top of the spec so you only need to define it once instead of including it throughout the spec. Failing that, define it again on line 71 (the line after describe "in the Users controller" do)

潜在的解决方案是在顶部定义 let(:user)..... 这样您只需定义一次 user 而不是在每个块中

Potential solution is defining let(:user)..... at the top so you only need to define user once instead of in each block

require 'spec_helper'

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

这篇关于Michael Hartl 的 Ruby on Rails 教程.第 9 章测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆