2 对于controller with invalid params的测试,一般对应以下的测试方法 Member.any_instance.stub(:delete).and_return(false) 3 控制器中方法的调用 subject.send(:log_in,@user)
----------------------------------------实例----------------------------- 1 控制器中的 before_filter class MyController < ApplicationController before_filter :logged_in?
def index end end
describe MyController do describe "GET 'index'" do context "when not logged in" # you want to be sure that before_filter is executed it "requires authentication" do controller.expects :logged_in? get 'index' end # you don't want to spec that it will redirect you to login_path # because that spec belongs to #logged_in? method specs end context "when authenticated" do before(:each) { controller.stubs :logged_in? } it "renders :index template" do get 'index' should render_template(:index) end it "spec other things your action does when user is logged in" end end end