RSpec + 形式 object + 简单形式给出未定义的方法

RSpec + form object + simple form gives undefined methods

我正在尝试对控制器的规范进行一些模拟。当我 post 有效文章时,我编写的代码可以很好地工作,但是当我尝试指定不应保存它时,我遇到了错误。

我的代码:

文章形式:

class ArticleForm
  include ActiveModel::Model
  delegate :title, :body, :author_id, :tags, :id, :persisted?, :new_record?, to: :article
  attr_accessor :article, :tags_string
  validates :title, :body, :tags, presence: true
  validates_length_of :title, within: 8..512
  validates_length_of :body, within: 8..2048
  validate :validate_prohibited_words

  def initialize(article = Article.new)
    @article = article
  end

  def save(article_params)
    assign_params_to_article(article_params)

    if valid?
      @article.tags.each(&:save!)
      @article.save!
      true
    else
      false
    end
  end
...
end

文章控制器(仅创建动作):

  def create
    @article_form = ArticleForm.new
    if @article_form.save(article_params)
      flash[:notice] = 'You have added a new article.'
      redirect_to @article_form.article
    else
      flash[:danger] = 'Failed to add new article.'
      render :new
    end
  end

_表格:

= simple_form_for @article_form,
  url: (@article_form.article.new_record? ? articles_path : article_path(@article_form.article) ) do |f|
  = f.input :title, label: "Article title:"
  = f.input :body, label: "Body of the article:", as: :text, input_html: { :style => 'height: 200px' }
  = f.input :tags_string, label: "Tags:", input_html: { value: f.object.all_tags }
  = f.button :submit, 'Send!'

文章控制器规格:

require 'rails_helper'

RSpec.describe ArticlesController, type: :controller do
  render_views

  let!(:user) { create(:user) }
  let!(:tag) { create(:tag) }
  let(:tags_string) { 'test tag' }
  let!(:article) { create(:article, :with_comments, tags: [tag], author_id: user.id) }


  context 'user logged in' do
    before { sign_in(user) }

    describe 'POST artictles#create' do
      let(:article_form) { instance_double(ArticleForm) }
      let(:form_params) do
        {
          article_form:
          {
            title: 'title',
            body: 'body',
            tags_string: tags_string
          }
        }
      end

      context 'user adds valid article' do
        it 'redirects to new article', :aggregate_failures do
          expect(ArticleForm).to receive(:new).and_return(article_form)
          expect(article_form).to receive(:save).with(hash_including(:author_id, form_params[:article_form]))
                                                .and_return(true)
          allow(article_form).to receive(:article) { article }

          post :create, params: form_params
          expect(response).to redirect_to(article)
        end
      end

      context 'user adds invalid article' do
        it 'renders new form', :aggregate_failures do
          expect(ArticleForm).to receive(:new).and_return(article_form)
          allow(article_form).to receive(:article) { article }

          expect(article_form).to receive(:save).with(hash_including(:author_id, form_params[:article_form]))
                                                .and_return(false)

          post :create, params: form_params
          expect(response).to render_template(:new)
        end
      end
    end
  end
end

发布有效工作正常,这是我在 'invalid post':

上遇到的错误

Failures:

1) ArticlesController user logged in POST artictles#create user adds invalid article renders new form Got 1 failure and 1 other error:

 1.1) Failure/Error: = simple_form_for @article_form,
        #<InstanceDouble(ArticleForm) (anonymous)> received unexpected message :model_name with (no args)
      # ./app/views/articles/_form.html.haml:2:in `_app_views_articles__form_html_haml__2443549359101958040_47338976410880'
      # ./app/views/articles/new.html.haml:2:in `_app_views_articles_new_html_haml___678894721646621807_47338976253400'
      # ./app/controllers/articles_controller.rb:26:in `create'
      # ./spec/controllers/articles_controller_spec.rb:51:in `block (5 levels) in <top (required)>'

 1.2) Failure/Error: = simple_form_for @article_form,

      ActionView::Template::Error:
        undefined method `param_key' for #<Array:0x0000561bedbcd888>
      # ./app/views/articles/_form.html.haml:2:in `_app_views_articles__form_html_haml__2443549359101958040_47338976410880'
      # ./app/views/articles/new.html.haml:2:in `_app_views_articles_new_html_haml___678894721646621807_47338976253400'
      # ./app/controllers/articles_controller.rb:26:in `create'
      # ./spec/controllers/articles_controller_spec.rb:51:in `block (5 levels) in <top (required)>'
      # ------------------
      # --- Caused by: ---
      # NoMethodError:
      #   undefined method `param_key' for #<Array:0x0000561bedbcd888>
      #   ./app/views/articles/_form.html.haml:2:in `_app_views_articles__form_html_haml__2443549359101958040_47338976410880'

我很少尝试通过允许 object 接收它来添加缺少的方法,但这是一件好事吗?稍后我将不得不允许每次调用(当我允许 param_keys 时,它会要求提供 _form - 标题、body 和标签的所有值)。有没有办法在不逐行指定所有方法的情况下让它工作?

使用 Rails 中的控制器测试 for "functional" testing。这意味着他们测试发送到应用程序的请求的多个层。

所以基本上您是在尝试测试处理请求所涉及的所有部分是否正常工作。因此,在这些类型的测试中不需要使用模拟和存根,因为您正在尝试使测试尽可能 "real" 。我建议实例化一个真正的 ArticleForm 对象,而不是创建一个双实例。

let(:article_form) { ArticleForm.new(article) } 

这样您也可以测试 ArticleForm 实例。在某些情况下,存根或模拟在控制器测试中也有意义,但在您的情况下, article_form 实例是测试的核心,因此对其使用模拟意味着大量工作和测试会变得更复杂。

如果您想尝试模拟,更好的起点可能是 view specs 例如。