如何在rspec Cabybara测试套件中设置参数?

时间:2014-06-18 13:43:20

标签: ruby-on-rails ruby rspec capybara

我的问题可能有多个答案。我还是新人。

我尝试做的是路由到设置了两个不同参数的页面。

/users/2/edit?type=edit
/users/2/edit?type=password

当我去那里时,我能够到达那里:

控制器代码:

def edit
    @edit_type = params[:type]
  end

查看代码:

<%= form_for(@user) do |f| %>
        <%= render 'shared/error_messages', object: f.object %>
        <% if current_user.admin? and @edit_type == 'edit' %>
            <%= f.label :username %>
            <%= f.text_field :username %>
        <% else%>
            <% if current_user?(@user) %>
                <%= f.label :curr_password, "Current Password" %>
                <%= f.password_field :curr_password %>
            <% end %>
            <%= f.label :password %>
            <%= f.password_field :password %>

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

        <% end %>
        <%= f.submit "Save changes", class: "btn btn-lg btn-primary" %>
    <% end %>

但是我如何从有关Capybara的rspec那里到达那里?

测试代码:

 describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    let(:admin) { FactoryGirl.create(:admin) }
    describe "page" do
      describe "as non-admin" do
        before do
          sign_in user
          visit edit_user_path(user)
        end
        it { should have_content("Update Password") }
        it { should have_title("Edit user") }
        it { should_not have_content("Username") }
      end
      describe "as admin " do
        # describe "visiting "
        before do
          sign_in admin
          visit edit_user_path(user)
        end
        # These all fail because it doesn't go to the right site (type is empty)
        it { should have_content("Update User Record") }
        it { should have_title("Edit user") }
        it { should  find_field("Username") }
      end
    end
. . .

如何在rspec / Capybara中设置参数(?type =&#39;某些&#39;)?

或者,我可以/应该路由到这个吗?类似的东西:

/users/:id/edit/:type

我该怎么做?我该如何测试

1 个答案:

答案 0 :(得分:3)

您可以将type参数传递给路线助手。在您的管理测试中:

visit edit_user_path(user)

应该是这样的:

visit edit_user_path(user, :type => "user")