Пытаясь построить правильное приспособление / фабрику из act_as_taggable_on

Нужна помощь, чтобы решить ошибку из теста rspec. Я просто не могу найти правильный способ построить фабрику или controller_spec для создания массива для tag_list.

модель:

class Record < ActiveRecord::Base
  #Associations
  belongs_to :user

  # Validations
  validates :title, :user, presence: true

  # Alias for acts_as_taggable_on :tags
  acts_as_taggable
end

controller_spec:

require 'rails_helper'

describe RecordsController do
  let(:record) { create(:record) }
  let(:title) { "Some title I would like to put in my record" }
  let(:description) { "description I would like to put in my record" }
  let(:tag_list) { ["tag", "list", "I", "would", "like", "to", "put", "in", "my", "record"] }


  describe "#create" do
    it "creates a new record with the given title, description and tag_list" do
      expect do
        post :create, record: { title: title, description: description, tag_list: tag_list }
      end.to change { Record.count }.by(1)

      expect(response).to redirect_to(assigns[:record])

      expect(assigns[:record].title).to eq(title)
      expect(assigns[:record].description).to eq(description)
      expect(assigns[:record].tag_list).to eq(tag_list)

    end

    it "fails to create a record and returns to the index page" do
      expect(post :create, record: { description: description, tag_list: tag_list }).to render_template(:index)
      expect(assigns[:records]).to eq(Record.all)
    end
  end
end

контроллер:

class RecordsController < ApplicationController

  def create
    @record = Record.new(record_params)

    if @record.save
      redirect_to @record
    else
      @records = Record.all
      render 'index'
    end
  end

  private

    def record_params
      params.require(:record).permit(:title, :description, :tag_list).merge(user: current_user)
    end
end

Фабрика:

FactoryGirl.define do
  factory :record do
    title 'record title'
    description 'record description'
    tag_list [create(:tag), create(:tag)]
    user
  end
end

ошибка из теста:

RecordsController
  #create
    creates a new record with the given title, description and tag_list (FAILED - 1)
    fails to create a record and returns to the index page

Failures:

  1) RecordsController#create creates a new record with the given title, description and tag_list
     Failure/Error: expect(assigns[:record].tag_list).to eq(tag_list)

       expected: ["tag", "list", "I", "would", "like", "to", "put", "in", "my", "record"]
            got: []

       (compared using ==)

1 ответ

Рассмотрите возможность изменения вашей спецификации на это.

require 'rails_helper'

describe RecordsController, type: :controller do
  context "#create" do
    it "creates a new record with the given title, description and tag_list parameters" do
      title = double(:title)
      description = double(:description)
      tag_list = double(:tag_list)
      record = double(:record)
      params = {record: record, title: title, description: description, tag_list: tag_list}
      expect(Record).to receive(:new).with(params).and_return(record)
      expect(record).to receive(:save)          
      post :create, params
      expect(assigns(:record)).to eq(record)
      expect(response).to redirect_to "/some_record_path"
    end

    it "fails to create a record and returns to the index page" do
      record = double(:record)
      records = double(:records)
      params = {record: record}
      expect(Record).to receive(:new).with(params).and_return(record)
      expect(Record).to receive(:all).and_return(records)          
      post :create, params
      expect(assigns(:records)).to eq(records)
      expect(response).to render_template :index
    end
  end
end

Примечание: я оставил тест выше, провалившись на линии redirect_to "some_record_path"

В тот момент, когда вы запускаете тест, если все остальное работает правильно, сбой скажет вам, каким должен быть путь. Или вы, наверное, уже знаете

Другие вопросы по тегам