[rspec-users] Stub a controller method containing arguments

16 views
Skip to first unread message

jeevan reddy

unread,
Sep 9, 2012, 2:00:25 AM9/9/12
to rspec...@rubyforge.org
controller code:

class BooksController < ApplicationController
def index
param1, param2 = "123", "456"
@test = method_1(param1, param2)
end

private
def method_1(param1, param2)
return "test"
end
end

controller tests

describe "GET index" do
it "get books" do
param1, param2 = "12334", "456233"
controller.stub!(:method_1).with(param1,param2).and_return("test")
get :index
assert_response :success
assigns(:test).should eq("test")
end
end

How to stub a controller private method which will take any arguments

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Sep 9, 2012, 7:31:56 AM9/9/12
to rspec...@rubyforge.org
On Sun, Sep 9, 2012 at 1:00 AM, jeevan reddy <li...@ruby-forum.com> wrote:
> controller code:
>
> class BooksController < ApplicationController
> def index
> param1, param2 = "123", "456"
> @test = method_1(param1, param2)
> end
>
> private
> def method_1(param1, param2)
> return "test"
> end
> end
>
> controller tests
>
> describe "GET index" do
> it "get books" do
> param1, param2 = "12334", "456233"
> controller.stub!(:method_1).with(param1,param2).and_return("test")
> get :index
> assert_response :success
> assigns(:test).should eq("test")
> end
> end
>
> How to stub a controller private method which will take any arguments

In your example above, the values of param1 and param2 are different
in the rspec example and the controller, so I'm guessing you're
getting a failure. If you're constraining a stub with specific args,
they have to match.

Curious - why do you care about the args in your real spec?
Reply all
Reply to author
Forward
0 new messages