2013-01-08 95 views
1

我對rspec很新穎。我如何編寫以下代碼段的功能測試。如何爲此編寫功能測試

class FooController < ApplicationController 
     def new 
    @title = "Log in to Mint" 
    @msg = session[:msg] 
    session[:msg] = nil 
    end 
end 

回答

2

怎麼是這樣的:

describe FooController do 

    describe "GET new" do 
    it "assigns 'Log in to Mint' to @title" do 
     get :new 
     assigns(:title).should == "Log in to Mint" 
    end 

    it "assigns message session to @msg" do 
     session[:msg] = "a message" 
     get :new 
     assigns(:msg).should == "a message" 
    end 

    it "sets message session to nil" do 
     get :new 
     session[:msg].should be_nil 
    end 
    end 

end 

參見:Rspec: testing assignment of instance variable