dgilperez
dgilperez

Reputation: 10796

Rspec conditional assert: have_content A OR have_content B

I know this would be a really newbie question, but I had to ask it ...

How do I chain different conditions using logic ORs and ANDs and Rspec?

In my example, the method should return true if my page has any of those messages.

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

Thanks for help!

Upvotes: 1

Views: 1909

Answers (1)

user324312
user324312

Reputation:

You wouldn't normally write a test that given the same inputs/setup produces different or implicit outputs/expectations.

It can be a bit tedious but it's best to separate your expected responses based on the state at the time of the request. Reading into your example; you seem to be testing if the user is logged in or authorized then showing a message. It would be better if you broke the different states into contexts and tested for each message type like:

# 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

Upvotes: 2

Related Questions