1

我读完了 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

4 回答 4

5

请注意 password_confirmation 标签文本是Confirm Password,您需要像下面这样更新规范:将 fill_in "Confirmation" 更改为:"foobar 到 fill_in "Confirm Password",使用:"foobar 我也遇到了这个问题,并通过上述方法解决方法。

于 2012-07-31T14:04:46.557 回答
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 %>

我通过查看Michael Hartl在 github 上的 index.html.erb 文件解决了最后两个错误。本质上,问题在于 index.html.erb 代码需要调用部分 _users.html.erb。

于 2012-06-24T01:12:23.323 回答
0

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

    <%= 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
于 2014-03-12T19:28:17.197 回答
0

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

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

错误

Capybara::ElementNotFound

表示它正在寻找要单击的按钮,但找不到它。如果你看代码

fill_in "Confirmation", with: "foobar"

然后你看看网页,上面写着确认密码。你可以通过输入让你的测试通过

fill_in "Confirm Password", with: "foobar"

您基本上只需要将水豚测试中的字符串与网页上显示的内容进行匹配即可。

于 2014-06-28T23:15:41.633 回答