Michael Hartl 的 Ruby on Rails 教程。第 9 章测试失败

2024-04-24

我是一名正在学习 Michael Hartl Ruby 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 

我的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: "[email protected] /cdn-cgi/l/email-protection") }
  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 行

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

以及上面的第 78 行

specify { response.should redirect_to(signin_path) }

有任何想法吗?我真的很困惑这意味着什么。谢谢Si。


在第 50 行你有

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

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

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


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

require 'spec_helper'

describe "authentication" do
  subject { page }
  let(:user) { FactoryGirl.create(:user) }
  ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Michael Hartl 的 Ruby on Rails 教程。第 9 章测试失败 的相关文章

  • 删除嵌套属性不起作用

    我似乎无法使用删除项目accepts nested attributes for命令 但我已经按照本教程 http railscasts com episodes 196 nested model form revised以及相关的git
  • Ruby 单引号和双引号

    我最近一直在使用 Ruby 进行编码 并且之前使用过 Python 据我所知 单引号和双引号对代码的工作方式没有影响 我转向 Ruby 是为了了解它是如何工作的 并研究 Ruby 和 Python 之间的相似之处 我曾经使用过单引号字符串并
  • Ruby 中的 Set 是否始终保留插入顺序?

    即 Ruby 的 Set 相当于 Java 的 LinkedHashSet 吗 在 Ruby 1 9 中 yes 在 Ruby 1 8 中 可能不会 Set uses a Hash内部 https github com ruby ruby
  • 向 Rails 应用程序中的内置类添加方法

    我想向 Rails 应用程序中的 Array 类添加一个方法 我应该把这个方法放在哪里 编辑得更清楚 显然我把它放在某个文件中 但是我如何告诉 Rails 应用程序在哪里可以找到它 执行此操作的一种方法是在以下位置创建一个文件lib rai
  • “rake db:seed”和 rake db:fixtures:load 之间有什么区别

    我是 Ruby 和 Rails 的新手 对某些事情感到好奇 在两个不同的教程中 我看到他们使用不同的方法用基本测试信息填充数据库 一种方法是使用 rake db seed 从包含示例数据的文本文件中提取数据 另一个使用 rake db fi
  • Rails simple_form:自定义输入id

    我想把用Rails simple form生成的几个表单放在一页上 并用javascript对它们进行操作 然而 simple form 为表单中的各个输入生成了相同的 id 因此我想用我自己的 id 替换生成的 id 现在我有一行 和 H
  • 不同金额的 Stripe 订阅计划

    我正在为一家慈善机构制作一份捐赠表格 他们要求提供每月捐赠计划 用户可以选择他们想要捐赠的任何金额 我知道我可以制定个人计划 即 如果他们说每月捐款 5 美元 10 美元或 20 美元 我可以制定三个不同的计划并向用户订阅它们 有没有办法避
  • 如何在测试环境中通过 URL 访问 ActiveStorage 对象?

    给定一个具有 ActiveStorage 附件的模型 class MyObject has one attached avatar end 在开发环境中 我能够将头像作为 StringIO 对象检索 obj MyObject new val
  • 查询参数和assert_generates/assert_routing - 我缺少什么?

    我想我已经介绍了使用查询参数测试路由的排列 但没有一种方法通过 在我的 paths rb 中 我有以下内容 resources items 然后对于我的功能测试我有 require ruby debug require test helpe
  • 如何设置管理员批准模型的编辑

    我需要一个普通用户可以编辑模型的系统 但编辑实际上只有在管理员批准后才会发生 我发现了一颗宝石 叫做纸迹 https github com airblade paper trail它确实有模型版本控制 但不具体支持我想要做的事情 我想知道其
  • 思考狮身人面像和控制台

    我在 webfaction 上思考 sphinx 时遇到问题 当我在 osx 上本地执行时没有问题 I search gt gt ThinkingSphinx 搜索 雷蒙德 我的回应是这样的 gt 有任何想法吗 thx sg 如果你还没有
  • 指定的 sqlite3 gem 未加载

    虽然我对 Ruby on Rails 比较陌生 但我开发应用程序已经有一段时间了 我似乎遇到的问题是 当我创建一个新的 Rails 应用程序 本地 使用 c9 时 当我启动 apache 服务器时 我似乎收到此错误 Specified sq
  • 防止语言环境文件中的 HTML 字符实体被 Rails3 xss 保护破坏

    我们正在构建一个应用程序 这是我们第一个使用 Rails 3 的应用程序 并且我们必须从一开始就构建 I18n 作为完美主义者 我们希望在我们的视图中使用真正的排版 破折号 卷曲引号 省略号等 这意味着在我们的 locales xx yml
  • File.delete 上的 Ruby (Errno::EACCES)

    我试图在使用完一些 XML 文件后删除它们 其中一个文件给了我这个错误 delete Permission denied monthly builds xml Errno EACCES Ruby 声称该文件受到写保护 但我在尝试删除它之前设
  • Facebook API 错误 100 - 无效链接

    我正在使用 Facebook API 在我的 Rails 应用程序中创建发送对话框 我只是在 Javascript 中使用 Facebook 推荐的格式 作为 HTML 中的脚本 我的问题是我得到 API Error code 100 in
  • 如何在控制器内部使用 auto_link

    在我的控制器中 我需要构建一个 JSON 对象 如何在控制器内使用 auto link 现在它错误 NoMethodError undefined method mail to for
  • 如何使用 Nokogiri 获取某些标签之后或之前的文本

    我有一个 HTML 文档 如下所示
  • 如何从模型调用辅助方法?

    我使用 MongoDB 作为我的 Rails 应用程序中的数据库和 MongoID gem 我想从模型中调用辅助方法after create回调方法 这怎么可能 我的模型代码是 class Department include Applic
  • 为什么这个 Ruby 方法返回“空值表达式”错误?

    我有这个简单的方法 def is palindrome sentence raise ArgumentError new expected string unless sentence is a String safe sentence s
  • 如何在 Ruby on Rails 中不使用 eval 将字符串转换为哈希值? [复制]

    这个问题在这里已经有答案了 这里是string需要转换成hash status gt label gt Status collection gt return misc definitions project status 我们不能使用ev

随机推荐