ruby-on-rails - rspec 中的 rails 命名空间 Controller 测试

标签 ruby-on-rails rspec controller namespaces

我的规范一般工作正常,但是当我尝试测试我的嵌套 Controller 时,我收到一个奇怪的错误。所以这个码型subject(:create_action) { xhr :post, :create, post_id: post.id, post_comment: attributes_for(:post_comment, post_id: post.id, user: @user) }与我的非嵌套 Controller 一起工作正常,但对于我的命名空间 Controller ,测试 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.rbspecs/controllers/posts文件夹并需要 posts/post_comments_controller ,但这没有帮助。

路由文件
resources :posts do
  resources :post_comments, only: [:create, :update, :destroy, :show], module: :posts
end

规范/ Controller /帖子/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

最佳答案

这是一个有点奇怪的错误。总之,您的 let!(:post) { ... }最终覆盖了一个名为 post 的方法在用于发出发布请求的示例组上。

发生这种情况是因为当您定义 let 时内describe , RSpec defines you a method given example group 内的同名.

describe Post do
  let(:post) { Post.new }

  it "does something" do
    post.do_something
  end
end

xhr method (以及 getpost 等),另一方面是用于从 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 方法。
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

关于ruby-on-rails - rspec 中的 rails 命名空间 Controller 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897874/

相关文章:

ruby-on-rails - 为什么 ActionDispatch::Request.original_url 返回错误的方案?

javascript - 我怎样才能让 URL 中的电子邮件地址显示在我的网页上,使用 javascript 或 HTML 构建在 Ruby on Rails 上的网站?

ruby-on-rails - unicorn 启动问题

ruby-on-rails - rswag gem 无法识别我在 rspec 中的参数

css - 无法在 css 文件中找到错误

php - 如何使用 silex 方法 get() 将参数从请求传递到 Controller 方法?

ruby-on-rails - 运行 rails new 时出现 Rubygems 问题

ruby-on-rails - 使用 rspec 检查错误的枚举

javascript - 在 AngularJS Controller 中获取 $resource 调用的值

unit-testing - 单元测试Grails Controller