如何解决ROR教程CH 9结束的rspec错误

时间:2012-06-23 22:56:48

标签: ruby-on-rails

我已经到了Michael Hartl的Ruby on Rails教程第9章的末尾,并且得到了失败的rspec测试。我已多次浏览这一章,找不到我做错了什么。请帮忙!另外,我是一个两周大的程序员,所以如果你能详细说明你经过调试的步骤,那将对我有很大的帮助!

我的git repo位于https://github.com/kerrieyee/sample_app,而rspec结果如下:

Macintosh-143:sample_app jeffreyyee$ bundle exec rspec spec/
..................................................................FF........FFFF.........

Failures:

  1) User pages index delete links as an admin user 
 Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
   expected link "delete" to return something
 # ./spec/requests/user_pages_spec.rb:45:in `block (5 levels) in <top (required)>'

  2) User pages index delete links as an admin user should be able to delete another user
 Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
 Capybara::ElementNotFound:
   no link with title, id or text 'delete' found
 # (eval):2:in `click_link'
 # ./spec/requests/user_pages_spec.rb:47:in `block (6 levels) in <top (required)>'
 # ./spec/requests/user_pages_spec.rb:47:in `block (5 levels) in <top (required)>'

  3) User pages signup with valid information should create a user
 Failure/Error: fill_in "Confirmation", with: "foobar"
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>'

  4) User pages signup with valid information after saving the user 
 Failure/Error: fill_in "Confirmation", with: "foobar"
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>'

  5) User pages signup with valid information after saving the user 
 Failure/Error: fill_in "Confirmation", with: "foobar"
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>'

  6) User pages signup with valid information after saving the user 
 Failure/Error: fill_in "Confirmation", with: "foobar"
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>'

Finished in 4.83 seconds
89 examples, 6 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:45 # User pages index delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:46 # User pages index delete links as an admin    user should be able to delete another user
rspec ./spec/requests/user_pages_spec.rb:99 # User pages signup with valid information should create a user
rspec ./spec/requests/user_pages_spec.rb:108 # User pages signup with valid information after saving the user 
rspec ./spec/requests/user_pages_spec.rb:109 # User pages signup with valid information after saving the user 
rspec ./spec/requests/user_pages_spec.rb:110 # User pages signup with valid information after saving the user 

4 个答案:

答案 0 :(得分:5)

请注意password_confirmation标签文本是确认密码,您需要修改以下规范:       更改       fill_in“确认”,附:“foobar       至       fill_in“确认密码”,附:“foobar 我也遇到了这个问题,并以上述方式解决了。

答案 1 :(得分:0)

我最终通过在new.html.erb和edit.html.erb中取出我渲染的部分来解决前4个失败的测试。因为我不是一个经验丰富的程序员,我不知道如何修复教程中的代码,并且会感谢任何能告诉我错误的人。这是重构的代码:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'fields', f: f %>
      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

这是部分:

<%= render 'shared/error_messages' %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :email %>
<%= f.text_field :email %>

<%= f.label :password %>
<%= f.password_field :password %>

<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>

我通过查看github上的Michael Hartl's index.html.erb file解决了最后两个错误。基本上,问题是index.html.erb代码需要调用partial _users.html.erb。

答案 2 :(得分:0)

确保标签中的文件:password_confirmation在user_pages_spec.rb和new.html.erb中都匹配。例如,您希望new.html.erb中的partial看起来像这样:

    <%= render 'shared/error_messages' %>
        <%= f.label :name %>
        <%= f.text_field :name %>
        <%= f.label :email %>
        <%= f.text_field :email %>
        <%= f.label :password %>
        <%= f.password_field :password %>
        <%= f.label :password_confirmation, "Confirm Password" %>
        <%= f.password_field :password_confirmation %>
        <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>

和user_pages_spec.rb是这样的:

    describe "with valid information" do
        before do
            fill_in "Name",             with: "Example User"
            fill_in "Email",            with: "user@example.com"
            fill_in "Password",         with: "foobar"
            fill_in "Confirm Password", with: "foobar"
        end
        it "should create a user" do
            expect { click_button submit}.to change(User, :count).by(1)
        end
    end

答案 3 :(得分:0)

Alex De Simone是对的我有同样的问题,他解决了。非常感谢。 Capybara做了我们所谓的浏览器测试。意思就像去这里点击那个。

作为我遇到的问题以及我如何解决它的一个例子。

错误

Capybara::ElementNotFound

意味着它正在寻找点击按钮而无法找到它。如果你看代码

fill_in "Confirmation", with: "foobar"

然后你看一下网页,然后说“确认密码”。您可以通过输入

来完成测试
fill_in "Confirm Password", with: "foobar"

你基本上只需要将你的水豚测试中的字符串与你在网页上看到的内容相匹配。