Rspec条件断言:have_content A或have_content B



我知道这是一个新手问题,但我不得不问…

如何使用逻辑or和and和Rspec链接不同的条件?

在我的例子中,如果我的页面有任何这些消息,该方法应该返回true。

def should_see_warning
  page.should have_content(_("You are not authorized to access this page."))
  OR
  page.should have_content(_("Only administrators or employees can do that"))
end

谢谢你的帮助!

您通常不会编写一个给定相同输入/设置产生不同或隐含输出/期望的测试。

这可能有点乏味,但最好根据请求时的状态将预期的响应分开。解读你的例子;您似乎在测试用户是否登录或授权,然后显示一条消息。如果您将不同的状态分解到上下文中,并对每种消息类型进行测试,就会更好,例如:

# logged out (assuming this is the default state)
it "displays unauthorized message" do
  get :your_page
  response.should have_content(_("You are not authorized to access this page."))
end
context "Logged in" do
  before
    @user = users(:your_user) # load from factory or fixture
    sign_in(@user) # however you do this in your env
  end
  it "displays a permissions error to non-employees" do
    get :your_page
    response.should have_content(_("Only administrators or employees can do that"))
  end
  context "As an employee" do
    before { @user.promote_to_employee! } # or somesuch
    it "works" do
      get :your_page
      response.should_not have_content(_("Only administrators or employees can do that"))
      # ... etc
    end
  end
end

相关内容

最新更新