【问题标题】:rspec: getting failure/error while redirecting to index actionrspec:重定向到索引操作时出现故障/错误
【发布时间】:2014-12-08 08:54:00
【问题描述】:

我是 rspec 的新手。我收到如下所示的失败消息:

Failures:

 1) PostsController PUT update it success when update a post
     Failure/Error: expect(response).to redirect_to(posts_url)
       Expected response to be a <redirect>, but was <200>
     # ./spec/controllers/posts_controller_spec.rb:11:in `block (3 levels) in <top (required)>'

Finished in 0.24448 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/posts_controller_spec.rb:8 # PostsController PUT update it success when update a post

posts_controller_spec.rb 中有以下代码

require 'spec_helper'

describe PostsController do

  describe "PUT update" do
    let(:post) {FactoryGirl.create(:post)}

    it "it success when update a post" do
      @post = FactoryGirl.create(:post, :title => "my title", :content => "my content")
      expect {put :update, :id => @post.id }
      // put :update, { :id => @post.id, :post => { "title" => "MyString", :content => "my content" } }
      expect(response).to redirect_to(posts_url)
    end
  end
end

我有很多解决方案,但没有成功。

我在这行遇到问题

  expect(response).to redirect_to(posts_url)

我也试过下面的代码

specify { expect(response).to redirect_to(posts_path) }

仅供参考,我已将以下代码添加到我的 spec_helper.rb

 RSpec.configure do |config|

  config.mock_with :rspec do |c|
    c.syntax = [:should, :expect]
  end
  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end

任何建议将不胜感激。

posts_controller.rb 是这样的。

    class PostsController < ApplicationController
        def edit
          @post = Post.find(params[:id]
        end

        def update
         @post = Post.find(params[:id])
         if @post.update_attributes(post_params)
           redirect_to posts_path
         else
           render :action => :edit
         end
        end

        def index
          @posts = Post.all
        end

        private
         def post_params
          params.require(post).permit!
         end
    end

我的帖子模型:

class Post < ActiveRecord::Base
  validates :title, :content, :presence => true
end

【问题讨论】:

  • 请告诉我们PostsController
  • @Зелёный:我添加了posts_controller,请看一下。
  • Post 上是否有任何验证?由于更新操作不成功,它似乎正在呈现编辑页面而不是重定向。
  • @AmitPatel:帖子模型包含字段的验证存在。
  • 看看下面的答案中解释的问题是什么。

标签: ruby-on-rails ruby-on-rails-3 rspec2 rspec-rails rspec3


【解决方案1】:

根据您的控制器,@post 从未更新,因为post_params 为空,所以update 动作渲染edit 模板状态为200

尝试更改您的 it 块以更新 @post 变量(这只是示例):

it "it success when update a post" do
  @post = FactoryGirl.create(:post, :title => "my title", :content => "my content", :id => 3)
  put :update, :id => @post.id, FactoryGirl.attributes_for(:post)
  expect(response).to redirect_to(posts_url)
end

【讨论】:

  • 非常感谢您提供详细信息。通过参数传递更新值后它工作正常。
  • @sivrj 很高兴,如果我帮助解决您的问题,请不要忘记接受答案。
  • 是的,我接受了你的回答。我将 a 行更新为类似这样,之后它工作正常>> put :update, { :id => @post.id, :post => { "title" => "MyString", :content => "我的内容”} }
猜你喜欢
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多