Ruby on rails 哈特尔';s RubyonRails教程测试失败(第9章)
我正在学习Hartl著名的RubyonRails4教程。我对Rails并不陌生,但我对测试是陌生的:RSpec、Capybara、FactoryGirl等等。我特别对编辑第9章()中的用户的测试有一个问题。我将先发布我试图补救的内容,然后发布相关代码:Ruby on rails 哈特尔';s RubyonRails教程测试失败(第9章),ruby-on-rails,rspec,capybara,factory-bot,Ruby On Rails,Rspec,Capybara,Factory Bot,我正在学习Hartl著名的RubyonRails4教程。我对Rails并不陌生,但我对测试是陌生的:RSpec、Capybara、FactoryGirl等等。我特别对编辑第9章()中的用户的测试有一个问题。我将先发布我试图补救的内容,然后发布相关代码: 与教程相比,我对代码进行了三次检查。看起来一切正常,但我可能遗漏了什么 重新启动我的服务器并重置、重新填充和重新设置测试数据库 使用FF中的SQLite Manager扩展检查测试和开发SQLite dbs中的用户 以下是错误: 1) Authe
1) Authentication Signin page with valid information followed by signout
Failure/Error: before { click_link "Sign Out" }
Capybara::ElementNotFound:
Unable to find link "Sign Out"
# ./spec/requests/authentication_pages_spec.rb:41:in `block (5 levels) in <top (required)>'
2) UserPages edit with invalid information
Failure/Error: before { click_button "Save changes" }
Capybara::ElementNotFound:
Unable to find button "Save changes"
# ./spec/requests/user_pages_spec.rb:130:in `block (4 levels) in <top (required)>'
3) UserPages edit page
Failure/Error: it { should have_title("Edit user") }
expected #has_title?("Edit user") to return true, got false
# ./spec/requests/user_pages_spec.rb:125:in `block (4 levels) in <top (required)>'
4) UserPages edit page
Failure/Error: it { should have_content("Update your profile") }
expected #has_content?("Update your profile") to return true, got false
# ./spec/requests/user_pages_spec.rb:124:in `block (4 levels) in <top (required)>'
5) UserPages edit page
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') }
expected #has_link?("change", {:href=>"http://gravatar.com/emails"}) to return true, got false
# ./spec/requests/user_pages_spec.rb:126:in `block (4 levels) in <top (required)>'
6) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>'
7) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>'
8) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>'
9) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>'
10) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
Unable to find field "Name"
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>'
我添加到最初的descripe块:js=>true
,因为我认为这是一个水豚问题,页面加载速度太快,但是,添加该哈希导致FF浏览器打开,我可以观察到发生了什么。。。FactoryGirl正在成功创建用户,但在尝试登录时收到无效的电子邮件或密码错误。因此它无法登录,因此编辑表单的所有测试都失败。以下是相关的FactoryGirl代码:
在./spec/utilities.rb中登录()帮助程序:
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign In"
end
end
./spec/factories.rb:
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
end
我已经正确安装了gem,FactoryGirl在其他测试中运行良好,下面是gem文件:
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'growl', '1.0.3'
gem 'factory_girl_rails', '4.2.1'
end
正如我所说,我查看了SQLite开发和测试数据库,但在测试数据库中没有看到任何用户。FactoryGirl是否只是临时为测试创建一个用户,然后在完成后回滚?什么会导致我看到的登录失败?还是我还遗漏了什么
如果有人问,以下是相关的观点:
edit.html.erb:
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a>
</div>
更新您的个人资料
以及在application.html.erb中呈现的_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", users_path %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign Out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign In", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
-
-
您遇到的第一个错误很可能是因为您需要在保存用户后将您的描述“然后注销”
嵌套在描述“中”
如下所示:
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user@example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
describe "followed by signout" do #Notice that this describe is inside the other one
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
我知道这只是部分答案,但很难一下子解决所有问题。解决此问题后,请再次运行测试,并告诉我保留了哪些错误代码。我会尽力进一步帮助你
(至少如果这解决了您的第一个问题,它可能是另一个问题)您遇到的第一个错误很可能是因为您需要在保存用户“后,将您的
描述“然后注销”
嵌套在描述”中,如下所示:
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user@example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
describe "followed by signout" do #Notice that this describe is inside the other one
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
我知道这只是部分答案,但很难一下子解决所有问题。解决此问题后,请再次运行测试,并告诉我保留了哪些错误代码。我会尽力进一步帮助你
(至少如果这解决了您的第一个问题,可能是其他问题)1规格错误在哪里?我想这就是登录的问题所在?错误2-11很可能是由此引起的,因此如果我们可以看到/修复此问题,其他错误可能会出现。是的,如果在上下文中的/块之前的块中执行,FactoryGirl会为特定测试或测试块创建用户。很抱歉,我应该声明,错误1与相同的测试无关,但可能是由相同的问题引起的:1)具有有效信息的身份验证登录页,随后是注销失败/错误:在{click_link“Sign Out”}之前Capybara::ElementNotFound:无法找到链接“注销”#./spec/requests/authentication\u pages\u spec.rb:41:in'block(5级)in'别担心。我没有看到用户登录时出现错误。你在哪里看到这个问题?当我通过命令行运行所有测试时,一旦它到达descripe'edit'
块,它就会启动firefox,我可以观察它的功能(因为我添加了hash:js=>true)。我实时观察它试图与FG生成的用户登录,并获得“无效电子邮件或密码”错误。1规范错误在哪里?我假设这就是登录问题所在?可能是错误2-11导致的,因此如果我们能够看到/修复,其他错误可能会出现。是的,FactoryGirl创建了特定测试或测试块的用户,如果在上下文/中的之前的块中完成,请描述块。完成后,工厂将被销毁。对不起,我应该说明,错误1与同一测试无关,但可能是由同一问题引起的:1)带有有效信息的身份验证登录页,后跟注销失败/错误:{单击链接“注销”}之前Capybara::ElementNotFound:找不到链接“注销”./spec/requests/Authentication\u pages\u spec.rb:41:`block(5级)在“别担心。我没有看到用户登录时出现错误。你在哪里看到这个问题?当我通过命令行运行所有测试时,一旦到达descripe'edit'
块,它就会启动firefox,我可以观察它做了什么(因为我添加了hash:js=>true)。我实时观察它试图与FG生成的用户登录,并获得“无效电子邮件或密码”错误。