user1066183
user1066183

Reputation: 2584

rspec test navbar links presence

I'm testing this partial:

<% if user_signed_in? %>
          <%= menu_item "Logout", destroy_user_session_path %>
<% else %>
          <%= menu_item "Login",new_user_session_path %>
<% end %>

This is my test:

require 'spec_helper'

describe "layouts/_header.html.erb" do

 before do
  view.stub(:user_signed_in?).and_return(true)
 end

 it "should have the right links on the header" do

 render
 rendered.should have_link('Login', new_user_session_path)
 rendered.should have_link('Logout', destroy_user_session_path)
 end
end

With this method I test only one if branch, the true branch.

How can I test all if branches?

Upvotes: 1

Views: 382

Answers (1)

fotanus
fotanus

Reputation: 20116

Use a context block to give an scope to your stub:

require 'spec_helper'

describe "layouts/_header.html.erb" do

 context "logged" do
   before do
    view.stub(:user_signed_in?).and_return(true)
   end

   it "should have the right links on the header" do
     render
     rendered.should have_link('Login', new_user_session_path)
   end
  end

 context "not logged" do
   before do
    view.stub(:user_signed_in?).and_return(false)
   end
   it "should have logout button" do
     render
     rendered.should have_link('Logout', destroy_user_session_path)
   end
 end
end

Upvotes: 1

Related Questions