【问题标题】:How to write a feature test for something that's determined by the user?如何为用户确定的内容编写功能测试?
【发布时间】:2015-10-30 13:37:01
【问题描述】:

假设我正在开发一个 cmets 功能,我想将它添加到我正在开发的应用程序中。

我可以通过功能测试来测试它:

scenario "they can comment on a book" do
  visit book_url(book)

  fill_in "Name", with: "John"
  fill_in "Comment", with: "This is a comment."
  click_button "Add Comment"

  expect(current_path).to eq(book_path(book))
  expect(page).to have_text("Your comment is successfully added")
  expect(page).to have_text(Comment.last.content)
end

但是如果我还添加一个功能,用户可以决定评论是否需要批准。如果不需要批准,则上述测试将起作用。但是,如果用户更改设置并决定评论在发布前需要获得批准,则此测试将不起作用(此设置可通过管理面板进行调整)。

编写涵盖所有这些场景的测试的好方法是什么?

【问题讨论】:

    标签: ruby-on-rails testing rspec tdd bdd


    【解决方案1】:

    您是否考虑过对每种情况进行单独的测试? IE。将不需要批准的情况与需要批准的情况分开测试。也许是这样的:

    feature "making comments when approval is not required" do
      background do
          // turn off the "approval required" setting
      end
    
      scenario "adding a comment" do
          // assertions
      end
    end
    
    feature "making comments when approval is required" do
      background do
          // turn on the "approval required" setting
      end
    
      scenario "adding a comment" do
        // assertions
      end
    end
    

    请注意,我没有使用 Ruby 或 RSpec——只是做了一些快速搜索——所以这可能不是你想要做的最惯用的方式。

    编辑:或类似的东西

    feature "making comments" do
      scenario "when approval is required" do
        // require approvals
        // assertions
      end
    
      scenario "when approval is not required" do
        // turn off approval requirement
        // assertions
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      相关资源
      最近更新 更多