我的規格一般工作正常,但是當我嘗試測試嵌套的控制器時,出現奇怪的錯誤。所以這個代碼模式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
勞拉,你救了我的命! –