2011-03-16 43 views
0

我在这里使用Rspec进行一些测试,我想确保控制器在某些操作中调用日志方法。我也在使用摩卡。在RSPEC功能测试中使用摩卡控制器

我想是这样的:

it "update action should redirect when model is valid" do 
    Tag.any_instance.stubs(:valid?).returns(true) 
    put :update, :id => Tag.first 
    controller.expects(:add_team_log).at_least_once 
    response.should redirect_to(edit_admin_tag_url(assigns[:tag])) 
    end 

是有什么作为“控制”变量使用?我试过自己,控制器类名...

+0

需要看到你的控制器代码来了解什么是怎么回事。否则,它看起来没有你的期望。 –

回答

0

我想你想要@controller而不是控制器。这里是我的测试套件的一个例子:

it "delegates to the pricing web service" do 
    isbn = "an_isbn" 
    @controller.expects(:lookup) 
    .with(isbn, anything) 
    .returns({asin: "an_asin"}) 

    get :results, isbn: isbn 
    assert_response :success 
end 
1

我刚刚得到了帮助。对于测试控制器,您可以将您的规格嵌套在名称为控制器的描述中。 (该规范也应在Controllers文件夹)

describe ArticlesController do 
    integrate_views 
    describe "GET index" do 
    ... 
     it "update action should redirect when model is valid" do 
     ... 
     controller.expects(:add_team_log).at_least_once 
    ... 
    end 
    end