【发布时间】: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