我该如何为此编写功能测试

时间:2013-01-08 05:28:12

标签: ruby ruby-on-rails-3 rspec rspec2

我对rspec很新。如何为下面的代码编写功能测试。

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

1 个答案:

答案 0 :(得分: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