rspec控制器测试期待路线

时间:2014-07-08 20:11:35

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails

我正在尝试测试创建帖子时,用户被重定向到部署路径。我添加了

  config.include Rails.application.routes.url_helpers

将rails路由扩展到rspec。但我的测试仍然失败,出现以下错误

1) Failure/Error: expect(response).to redirect_to(path)
   Expected response to be a redirect to <http://test.host/deployments/new> but was a redirect to <http://test.host/deployments/new.1473>.
   Expected "http://test.host/deployments/new" to be === "http://test.host/deployments/new.1473".
     # -e:1:in `<main>' 

以下是测试:

  describe "post create" do
    before do
      allow(model).to receive(:new).and_return(instance)
    end
    context "where all is not well" do
      before do
        allow(instance).to receive(:save).and_return(false)
        post :create, params_new_instance
      end
      sets_flash(:error)
      it "should render the new form" do
        expect(response).to render_template("projects/new")
      end
    end
    context "where all is well" do
      before do
        allow(instance).to receive(:save).and_return(true)
        post :create, params_new_instance
      end
      sets_flash(:notice)
      it "redirects to new_deployments_path" do
        expect(controller.controller_path).to eq(new_deployment_path)
      end
    end
  end

项目控制器

  def create
    @project=Project.new(params_project)
    if @project.save
      record_saved
      return redirect_to(new_deployment_path(@project))
    else
      check_for_errors
      return render('/projects/new')
    end 
  end

为什么会失败?我是以正确的方式接近这个吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

您确定new_deployments_path代替new_deployment_path 这是我的第一个建议。它们可以是更多但信息不足的信息:routes.rb部署控制器,例如