简体   繁体   English

如何在带有MiniTest的Rails 3.2中模拟“编辑”或“销毁”按钮的单击?

[英]How do I simulate an “Edit” or “Destroy” button click in Rails 3.2 w/ MiniTest?

How can I write an integration test for editing an item? 如何编写用于编辑项目的集成测试? My "create" test looks like this: 我的“创建”测试如下所示:

  it "lets a user create a product" do
    login_user
    click_link("Products")
    click_link("New")
    fill_in "Identifier", :with => "MyString"
    click_button "Create"
    assert page.has_content?("Product was successfully created")
  end

And that works great. 而且效果很好。 What I am confused about is how to do the Edit and Destroy tests. 我感到困惑的是如何进行“编辑”和“销毁”测试。 My index page provides a list of all products. 我的索引页面提供了所有产品的列表。 So first I use a factory to create a couple of products. 因此,首先,我使用一家工厂来创建几​​个产品。 Now I am in the situation where there are multiple "Edit" and "Destroy" buttons. 现在,我处于多个“编辑”和“销毁”按钮的情况。 I can't just say: 我不能只说:

click_button "Destroy"

because there are two of them. 因为有两个 How do I tell it which one to click? 如何告诉我要点击哪个?

And if I do get the correct "Destroy" button clicked, how do I hit the "OK" button in the Javascript window that pops up? 如果我确实单击了正确的“销毁”按钮,如何在弹出的Javascript窗口中单击“确定”按钮?

Assuming that you're using Webrat, you can use the "within" selector. 假设您正在使用Webrat,则可以使用“内部”选择器。

The Webrat "within" method takes a CSS selector as an argument. Webrat的“内部”方法将CSS选择器作为参数。 Supposing your "Destroy" button is in a div with an id like "#product-2", you can isolate that button with: 假设您的“销毁”按钮位于一个ID为“#product-2”的div中,则可以使用以下命令隔离该按钮:

within "#product-2" do |scope|
  scope.click_button "Destroy"
end

If you need to / would rather use XPath, you can do something like: 如果您需要/宁愿使用XPath,则可以执行以下操作:

 response.should have_xpath(xpath) do |button|
   click_button(button)
 end

Alternatively, if you are using Capybara, then you can use the "find" method: 另外,如果您使用的是水豚,则可以使用“查找”方法:

find("#product-2").find("button").click
find(:xpath, "//div/div/button").click

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM