我的问题可能有多个答案。我还是新人。
我尝试做的是路由到设置了两个不同参数的页面。
/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
. . .
或者,我可以/应该路由到这个吗?类似的东西:
/users/:id/edit/:type
我该怎么做?我该如何测试
答案 0 :(得分:3)
您可以将type
参数传递给路线助手。在您的管理测试中:
visit edit_user_path(user)
应该是这样的:
visit edit_user_path(user, :type => "user")