【发布时间】:2013-04-07 08:52:02
【问题描述】:
我四处搜索,这似乎是一个很常见的问题,但对我没有任何帮助。来了!
首先,我正在学习 Hartl RoR 教程。我即将结束section 9(视频905),我正在尝试测试失败的用户登录。这是user_spec.rb中的代码:
describe "failure" do
it "should not log the user in" do
visit login_path
puts response.body
fill_in 'session_email', with: ""
fill_in 'session_password', with: ""
click_button
response.should have_selector('div.login_error', content: "Invalid")
response.should render_template('sessions/new')
end
end
这是自动测试错误:
1) User login failure should not log the user in
Failure/Error: visit login_path
NameError:
undefined local variable or method `login_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x00000102c79248>
# ./spec/models/user_spec.rb:176:in `block (4 levels) in <top (required)>'
我尝试将visit login_path 写为visit '/login',但仍然出现错误,尽管它已更改为:
Failure/Error: visit '/login'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x00000102c2d140>
这让我觉得这可能是一个 webrat 问题,因为我在某处读到“访问”来自 webrat(而不是 rspec)。我也知道它没有超过第一行,因为puts response.body 没有出现在自动测试失败中。更令人困惑的是,在我的应用程序的另一部分,我有其他几乎相同的测试代码,并且运行良好。
layout_links_spec.rb:
before(:each) do
@user = Factory(:user)
visit login_path
# puts response.body
fill_in 'session_email', with: @user.email
fill_in 'session_password', with: @user.password
click_button
end
有什么想法吗?
【问题讨论】:
-
您是否错过了 user_spec.rb 中的任何包含语句。比较 layout_links_spec.rb 和 user_spec.rb 中的包含
-
两者都有
require 'spec_helper' -
我觉得这里有些不对劲。看看测试
User模型的the Rails Tutorial user_spec.rb;没有*_path测试。 Capybara 测试visit和page将在 requests/user_pages_spec.rb 中。 -
hmm...我没有
user_pages_spec.rb- 我也没有使用Capybara,也许我使用的是不同的版本? Rails Tutorial user_spec.rb 的语法与我的语法不同 - 这里是 my user_spec.rb。 -
啊,抱歉,我的意思是说'也许我正在使用不同版本的 Hartl 教程'
标签: ruby-on-rails rspec ruby-on-rails-3.2 railstutorial.org webrat