0

我正在尝试对控制器进行以下功能测试。我正在使用以下

  • RSpec2
  • 导轨 3
  • 舒德拉
  • 摩卡

这是测试

context "POST on create should be successful" do
  before(:each) do
    Model.any_instance.expects(:save).returns(true).once
    Model.any_instance.stubs(:id).returns(1)
    post :create, :model => {}
  end

  it { should assign_to(:model).with_kind_of(Model) }
  it { should set_the_flash.to("Model was created successfully.")}
  it { should redirect_to(model_path(1))}
end

被测控制器是

def create
  @model = Model.new(params[:model])

  if @model.save
    flash[:success] = "Model was created successfully."
  end
  respond_with @model
end

唯一失败的测试是第三个测试,它表示返回的路径是“http://test.host/models”而不是“http://test.host/models/1”

当我在浏览器中运行应用程序时,我得到了正确的重定向。

4

1 回答 1

0

我认为您也需要模拟 to_params 。它由路线系统使用

context "POST on create should be successful" do
  before(:each) do
    Model.any_instance.expects(:save).returns(true).once
    Model.any_instance.stubs(:id).returns(1)
    Model.any_instance.stubs(:to_params).returns(1)
    post :create, :model => {}
  end

  it { should assign_to(:model).with_kind_of(Model) }
  it { should set_the_flash.to("Model was created successfully.")}
  it { should redirect_to(model_path(1))}
end
于 2010-10-14T15:49:31.337 回答