我的规格一般工作正常,但是当我尝试测试嵌套的控制器时,出现奇怪的错误。所以这个代码模式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
引发以下错误:rspec中的rails命名空间控制器测试
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
劳拉,你救了我的命! –