0

我正在将我的用户身份验证从头开始转换为设计 gem。一切都已完成,似乎工作正常。我改变了我的 rspec 测试,但是我有一个反复出现的问题,我已经搜索过一个解决方案。

错误

2) AuthenticationPages authorization for non-signed-in users when attempting to visit a    protected page after signing in should render the desired protected page
     Failure/Error: fill_in :email,    with: user.email
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label     'email' found
     # (eval):2:in `fill_in'
     # ./spec/requests/authentication_pages_spec.rb:53:in `block (5 levels) in <top (required)>'

考试

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_registration_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

设计表单(new_user_session - 在被允许查看编辑用户信息之前出现)

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
  <%= f.label :email %>
  <%= f.email_field :email, :autofocus => true %>

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

  <% if devise_mapping.rememberable? -%>
    <%= f.check_box :remember_me %> <%= f.label :remember_me %> 
  <% end -%>

  <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<%= render "devise/shared/links" %>

在浏览器中进行测试时,该行为实际上可以正常工作,但 rspec 测试失败。

4

1 回答 1

0

Capybara 找不到电子邮件的输入;由于表单似乎正确构建,Capybara 很可能在到达表单之前被重定向到另一个页面。

您确定要遵循表单的路径是edit_user_registration_path(user),而不是例如new_user_registration_path或(如果您正在测试 sign_in 而不是 sign_up,那就更好了)new_user_session_path

于 2013-05-08T17:15:01.297 回答