gpt4 book ai didi

ruby-on-rails - Rspec 条件断言 : have_content A OR have_content B

转载 作者:行者123 更新时间:2023-11-28 20:33:18 24 4
gpt4 key购买 nike

我知道这将是一个真正的新手问题,但我不得不问...

如何使用逻辑 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

关于ruby-on-rails - Rspec 条件断言 : have_content A OR have_content B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com