【问题标题】:Ruby on Rails - Rspec 3.8, Rails 5, FacotryBot POST redirect test failureRuby on Rails - Rspec 3.8、Rails 5、FacotryBot POST 重定向测试失败
【发布时间】:2018-11-23 18:03:28
【问题描述】:

我正在关注此处的 Ruby on Rails 餐厅教程:http://rubyonrailstutor.github.io/。首先,我不得不通过反复试验和在线搜索错误进行一些更改。这在很大程度上是因为它是关于旧版本 Ruby 和 Rails 的旧教程。

我正在使用 rails 5.2.1 和 Ruby 2.5.3 以及大多数 gem 的最新版本。我已经成功完成了本教程,直到最后一次在这里找到rspec 测试:http://rubyonrailstutor.github.io/new/restaurants-new-create/。

当我运行rspec spec/requests/restaurants_spec.rb 时出现以下错误:

Failures:

  1) RestaurantsController POST /restaurants complete params redirects to show
     Failure/Error: expect(subject).to redirect_to restaurant_path(id:1)

       Expected response to be a redirect to <http://www.example.com/restaurants/1> but was a redirect to <http://www.example.com/restaurants/new>.
       Expected "http://www.example.com/restaurants/1" to be === "http://www.example.com/restaurants/new".
     # ./spec/requests/restaurant_spec.rb:32:in `block (4 levels) in <top (required)>'

Finished in 0.60375 seconds (files took 1.47 seconds to load)
5 examples, 1 failure

Failed examples:

rspec ./spec/requests/restaurant_spec.rb:31 # RestaurantsController POST /restaurants complete params redirects to show

我的控制器和教程作者使用的一样:https://github.com/rubyonrailstutor/restaurantly/blob/restaurants-new/app/controllers/restaurants_controller.rb。

我的规范文件中失败的部分是:

context "POST /restaurants" do
    context "complete params" do
      subject{ post "/restaurants", params: { name: "mcrails"}}
      it "redirects to show" do
        expect(subject).to redirect_to restaurant_path(id:1)
      end 
    end 

    context "incomplete params" do
      subject{ post "/restaurants", {}} 
      it "redirects to new" do
        expect(subject).to redirect_to(new_restaurant_path)
      end 
    end 
  end 

我的主题与作者使用它的方式略有不同,我想知道这是我弄错的地方。当我收到一些我认为是由于版本差异引起的参数错误时,我更改了它。原文是这样的:

restaurant = {restaurant: {name: "mcrails"}}
subject{ post "/restaurants", restaurant }

我改成这样了:

subject{ post "/restaurants", params: { name: "mcrails"}}

作者在 rails 4 上的规范文件: https://github.com/rubyonrailstutor/restaurantly/blob/restaurants-new/spec/requests/restaurant_spec.rb.

当我运行 Web 服务器并创建一个新项目时,它会正确重定向到 #show 该项目。但是 rspec 测试失败。所以我相信我没有正确运行测试。也许是由于版本差异和我传递的参数,但我无法弄清楚这一点。在这一点上,我的 Rails 经验非常少。

【问题讨论】:

  • 结帐their documentation。它提供了 post/pre rails 5 的示例。
  • @MarkMerritt 感谢您的链接。我之前在他们的request-specs 文档页面上找到了类似的示例。但未能弄清楚。使用您的链接,我花了更多时间尝试修改语法,看起来我终于能够让它按预期工作!非常感谢。

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


【解决方案1】:

@MarkMerritt 向我指出了一些文档,这些文档帮助我进行了一些尝试和错误,以弄清楚如何使其工作。

虽然我仍然不完全理解本教程之间的语法差异以及我能够开始工作的内容,但我会发布我的最终规范文件编辑,以防有人有任何反馈。

context "POST /restaurants" do
    context "complete params" do
      subject { post "/restaurants", :params => { :restaurant => { :name => "mcrails" } } } 

      it "redirects to show" do
        expect(subject).to redirect_to restaurant_path(:id => assigns(:restaurant).id) 
      end 
    end 

    context "incomplete params" do
      subject { post "/restaurants", :params => {} } 

      it "redirects to new" do
        expect(subject).to redirect_to(new_restaurant_path) 
      end 
    end 
  end

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-01-25
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
相关资源
最近更新 更多