Ruby on rails 使用RSpec和Rails测试带有参数的方法调用
我有这个Rails API,我正在尝试在我的控制器中测试两个操作。第一个应该叫第二个。如何使用RSpec实现这一点 以下是行动:Ruby on rails 使用RSpec和Rails测试带有参数的方法调用,ruby-on-rails,rspec,Ruby On Rails,Rspec,我有这个Rails API,我正在尝试在我的控制器中测试两个操作。第一个应该叫第二个。如何使用RSpec实现这一点 以下是行动: def method1 (...) method2(@example_var) end def method2(id) (...) end 到目前为止,我已经试过了,但没有成功 describe "#method1" do it "calls the method2 with id" do Rails::logger
def method1
(...)
method2(@example_var)
end
def method2(id)
(...)
end
到目前为止,我已经试过了,但没有成功
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
有什么帮助吗
编辑:如果信息不够,我很乐意提供更多
method2
未在父级上调用。它将在控制器实例上调用method2
是私有方法吗?如果它是一个操作,您应该重定向到该操作,而不是调用该方法method2
调用上设置期望值,请执行以下操作
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
这不是一个确切的答案,但这正是我的意图和解决方案:)希望这能帮助有需要的人 测试:
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
控制员:
def close
(...)
redirect_to :controller => 'Parents', :action => 'method2', :field1 => @parent[:field1], :field2 => @parent[:field2], :field3 => @parent[:field3]
end
1.请包括更多的控制器。类是父类吗?2.我认为在执行期望触发它的操作(调用get:method1)之前,需要设置期望值(应该接收)。3.你为什么要显式地调用method2?我同时解决了我的问题,但因为我对Rails非常陌生,所以我把一切都搞砸了。谢谢你的回复。谢谢你的努力!我已经解决了这个问题。请阅读我上面的评论。我会投票给你,但我还没有足够的声誉:(