RSpec +表单对象+简单表单给出了未定义的方法

时间:2018-05-19 11:55:56

标签: ruby-on-rails rspec simple-form

我试图在控制器的规格中做一些嘲弄。我编写的代码在我发布有效文章的情况下工作得很好,但是当我尝试指定它不应该被保存时,我面临错误。

我的代码:

文章形式:

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

_form:

= 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

发布有效的作品正常,这是我遇到的错误'无效的帖子':

  

故障:

     

1)ArticlesController用户登录POST artictles #create user adds   无效文章呈现新表格        得到1个失败和另外1个错误:

 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'

我几乎没有尝试通过允许对象接收它来添加缺失的方法,但它是一件好事吗?我必须稍后允许每一次调用(当我允许param_keys时,它会询问_form的所有值 - 标题,正文和标签)。有没有办法让它工作而不是逐行指定所有方法?

1 个答案:

答案 0 :(得分:0)

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

所以基本上你试图测试处理请求所涉及的所有部分。因为在这些测试中不需要使用模拟和存根,因为你试图将测试作为真实的&#34;尽可能。我建议实例化一个真正的ArticleForm对象,而不是创建一个双实例。

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

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

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