【发布时间】:2017-04-07 06:02:09
【问题描述】:
我正在学习如何在 Rails 中测试控制器。我的帖子控制器中有此操作:
def update
@post = Post.new(post_params)
if @post.save
redirect_to posts_path
flash[:success] = "Your post has been updated"
else
render 'edit'
end
end
相当基本的更新操作。我想测试一下。这是我现在的测试:
需要'rails_helper'
RSpec.describe PostsController, type: :controller do
let!(:test_post) { Post.create(title: "testing", body: "testing") }
describe "PUT update" do
context "when valid" do
it "updates post" do
patch :update, id: test_post, post: {title: 'other', body: 'other'}
test_post.reload
expect(test_post.title).to eq('other')
end
end
end
end
此测试未通过。这是我从 RSpec 得到的错误:
1) PostsController PUT update when valid updates post
Failure/Error: expect(test_post.title).to eq('other')
expected: "other"
got: "testing"
(compared using ==)
我希望得到一些指导。谢谢!
【问题讨论】:
-
你在做
post :update而不是patch :update。 Rails 使用put/patch更新记录。 -
我实际上是不小心把它留在里面的。改变了它,它仍然失败。有什么想法吗?
标签: ruby-on-rails rspec