我的规格一般都很好,但是当我尝试测试我的嵌套控制器时,我得到了一个奇怪的错误。所以这个代码模式subject(:create_action) { xhr :post, :create, post_id: post.id, post_comment: attributes_for(:post_comment, post_id: post.id, user: @user) }
适用于我没有嵌套的控制器,但是对于我的命名空间控制器,测试it "saves the new task in the db
会引发以下错误:
1) Posts::PostCommentRepliesController when user is logged in POST create with valid attributes saves the new task in the db
Failure/Error: subject(:create_action) { xhr :post, :create, post_comment_id: post_comment.id, post: attributes_for(:post_comment_reply, post_comment: post_comment, user: @user) }
ArgumentError:
wrong number of arguments (4 for 0)
我该怎么做才能使这项工作成功?如您所见,我将post_comments_controller_spec.rb
放在specs/controllers/posts
文件夹中并要求posts/post_comments_controller
,但它没有帮助。
的routes.rb
resources :posts do
resources :post_comments, only: [:create, :update, :destroy, :show], module: :posts
end
规格/控制器/帖/ post_comments_controller_spec.rb
require "rails_helper"
require "posts/post_comments_controller"
describe Posts::PostCommentsController do
describe "when user is logged in" do
before(:each) do
login_user
end
it "should have a current_user" do
expect(subject.current_user).to_not eq(nil)
end
describe "POST create" do
let!(:profile) { create(:profile, user: @user) }
let!(:post) { create(:post, user: @user) }
context "with valid attributes" do
subject(:create_action) { xhr :post, :create, post_id: post.id, post_comment: attributes_for(:post_comment, post_id: post.id, user: @user) }
it "saves the new task in the db" do
expect{ create_action }.to change{ PostComment.count }.by(1)
end
end
end
end
end
答案 0 :(得分:2)
这是一个有点奇怪的错误。简而言之,您的let!(:post) { ... }
最终覆盖了用于发布帖子请求的示例组中名为post
的方法。
这是因为您在defines you a method内的同名let
,RSpec given example group内定义了describe
。
describe Post do
let(:post) { Post.new }
it "does something" do
post.do_something
end
end
另一方面, xhr
method(以及get
,post
等)是从Rails本身进行测试的辅助方法。带有帮助程序的RSpec includes this module,以便在测试中可用。
describe PostController, type: :controller do
let(:comment) { Comment.new }
it "can post a comment thanks to Rails TestCase::Behaviour" do
post :create, comment
# xhr :post, ... under the covers calls for post method
end
end
因此示例组对象有一个名为post的方法,在创建let时放置,RSpec去了并在组对象上创建了一个同名的方法,从而覆盖原始(并且非常需要)的post方法。 / p>
describe PostController, type: :controller do
let(:post) { Post.new }
it "mixes up posts" do
post :create, post # post now refers to the variable, so this will cause an error
end
end
要轻松解决此问题,我建议您将let(:post)
命名为其他内容,例如new_post
。
describe PostController, type: :controller do
let(:new_post) { Post.new }
it "does not mix up posts" do
post :create, new_post # no more conflict
end
end