1

我正在测试这个部分:

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

这是我的测试:

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

使用这种方法,我只测试一个if分支,即真正的分支。

如何测试所有if分支?

4

1 回答 1

1

使用上下文块为您的存根提供范围:

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
于 2013-10-24T14:18:45.827 回答