I have this Rails API and I'm trying to test two actions in my controller. The first one is supposed to call the second one. How can I do this using RSpec?
These are the Actions:
def method1
(...)
method2(@example_var)
end
def method2(id)
(...)
end
So far, I've tried this, unsuccessfully
describe "#method1" do
it "calls the method2 with id" do
Rails::logger.info "Testing METHOD1"
var = Parent.create(FactoryGirl.attributes_for(:valid_attributes))
get :method1, :id => var.id
Parent.should_receive(:method2).with(var.id)
Parent.method2
end
end
Any help?
EDIT: If information is not enough, I will gladly provide more.
method2
is not called on Parent
. It will be called on the controller instance method2
a private method? If its an action, you should redirect to the action instead of calling the method. But if you want to set an expection on the method2
call, do the following
describe "#method1" do
it "calls the method2 with id" do
Rails::logger.info "Testing METHOD1"
var = Parent.create(FactoryGirl.attributes_for(:valid_attributes))
@controller.should_receive(:method2).with(var.id)
get :method1, :id => var.id
end
end
It's not exactly an answer, but this was what I intended and the solution to it :) Hope this can help someone who needs it
The test:
describe "GET #method1" do
it "gets a parent by :id and redirect_to #method2" do
Rails::logger.info "Testing METHOD1 with redirect to METHOD2"
vars = Parent.create(FactoryGirl.attributes_for(:valid_params))
expect redirect_to('method2/' + vars.field1 + '/' + vars.field2 + '/' + vars.field3)
end
end
The controller:
def close
(...)
redirect_to :controller => 'Parents', :action => 'method2', :field1 => @parent[:field1], :field2 => @parent[:field2], :field3 => @parent[:field3]
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.