【问题标题】:rspec controller test expecting routerspec 控制器测试期望路由
【发布时间】:2014-07-08 20:11:34
【问题描述】:

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

  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

为什么会失败?我是否以正确的方式处理这个问题?

提前致谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 现在在“重定向到 new_deployments_path”块中,您需要替换 eq(new_deployment_path) smth。喜欢这里:eq(new_deployment_path(:post))。对不起,现在我的眼睛再也睁不开了。现在在我的乡村之夜。但我想你明白我的意思。您需要变量来获得正确的测试路径new_deployments_path
    猜你喜欢
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多