我如何构建这个 Rails 集成测试(使用 Shoulda)以便“访问”不是每次都叫吗?

发布于 2024-11-26 18:54:22 字数 1158 浏览 2 评论 0原文

使用 Test::Unit 和 Shoulda,我想编写一个具有明确期望的测试,所以我想出了这个:

context "logged in as seller" do
  setup do
    login @seller
    visit offer_path(@seller_offer)
  end

  should "not see feedback" do
    assert has_no_selector?("#feedback")
  end

  should "see 'cancel offer' button" do
    assert has_selector?("#cancel_offer_button")        
  end

  should "see 'comment' button" do
    assert has_selector?("#comment_button")
  end

  should "not see 'accept offer' button" do
    assert has_no_selector?("#accept")
  end

end

问题是,在每个“should”块之前,设置会重新执行 - 这意味着两个页面请求(一个用于登录助手,另一个用于调用“访问”)。

我尝试做

context "logged in as seller" do
  login @seller
  visit offer_path(@seller_offer)

  should ...

但这似乎不起作用...显然我可以做:

context "logged in as seller" do
  should "have desired results" do
    login @seller
    visit offer_path(@seller_offer)

    # should see 'cancel offer' button
    assert has_selector?("#cancel_offer_button")        

    # should see 'comment' button"
    assert has_selector?("#comment_button")

    etc..

但这并不完全是我正在寻找的。

-帕特里克

Using Test::Unit and shoulda, I wanted to write a test that has expectations that are clear and so I came up with this:

context "logged in as seller" do
  setup do
    login @seller
    visit offer_path(@seller_offer)
  end

  should "not see feedback" do
    assert has_no_selector?("#feedback")
  end

  should "see 'cancel offer' button" do
    assert has_selector?("#cancel_offer_button")        
  end

  should "see 'comment' button" do
    assert has_selector?("#comment_button")
  end

  should "not see 'accept offer' button" do
    assert has_no_selector?("#accept")
  end

end

The problem is, before each "should" block, the setup is re-executed-- meaning a two page requests (one for the login helper, and the other for the call to "visit").

I tried doing

context "logged in as seller" do
  login @seller
  visit offer_path(@seller_offer)

  should ...

But that does not seem to work... Obviously I could do:

context "logged in as seller" do
  should "have desired results" do
    login @seller
    visit offer_path(@seller_offer)

    # should see 'cancel offer' button
    assert has_selector?("#cancel_offer_button")        

    # should see 'comment' button"
    assert has_selector?("#comment_button")

    etc..

But that's not exactly what I am looking for.

-patrick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸印温柔 2024-12-03 18:54:22

对于初学者:如果您希望测试只测试一件事,正如它应该的那样,那么这个问题没有解决方案,并且您已经在做正确的事情了。

不是进行完整的集成测试,并在集成测试中测试视图(页面上每个元素的外观)

  • 但是,还有其他方法:我通常执行以下操作:测试控制器,没有视图,最好没有视图,而 访问数据库(使用存根)。
  • 单独测试视图:我在那里测试所有内容
  • 在集成测试中是否正确呈现:
    • 使用黄瓜
    • 采用快乐路径点击方法(错误等已单独测试)
    • 重点关注 JavaScript 部分

希望这会有所帮助。

For starters: if you want your tests to test only one thing, as it is supposed to be, there is no solution to this problem, and you are already doing the right thing.

However, there are alternative approaches: instead of doing the full integration test, and testing the view (the appearance of each element on the page) in the integration test, I generally do the following:

  • test the controller, without the views, preferably without hitting the database (using stubs).
  • test the views in isolation: there I test that everything is rendered correctly
  • in the integration tests:
    • use cucumber
    • do a happy-path click-thru approach (errors and such have been tested separately)
    • focus on the javascript parts

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文