尝试从acts_as_taggable_on 构建正确的夹具/工厂
Posted
技术标签:
【中文标题】尝试从acts_as_taggable_on 构建正确的夹具/工厂【英文标题】:trying to build the right fixture / factory from acts_as_taggable_on 【发布时间】:2016-01-13 19:14:13 【问题描述】:需要一些帮助来解决 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
控制器规格:
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 ==)
【问题讨论】:
你能把记录模型也添加到问题中吗 如果您想将数组保存到数据库,请尝试将“serialize :tag_list”添加到您的模型中。 让我知道它是否有效? 还是出现同样的错误,要么是controller_spec的问题,要么是工厂的问题。 【参考方案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"
这条线上留下了上面的测试失败
如果其他一切正常,则在您运行测试的那一刻,失败将告诉您路径应该是什么。或者你可能已经知道了
【讨论】:
嗨 MilesStanfield,我知道了: 8) RecordsController#create 使用给定的标题、描述和 tag_list 参数创建一条新记录失败/错误:expect(assigns(:record)).to eq(record ) 预期:#<:mocks::double:0x8447cbb8> 得到:nil(使用 == 比较) 9) RecordsController#create 未能创建记录并返回索引页面失败/错误:预期(assigns(:records)).to eq(records) 预期:#<:mocks::double:0x817382c4> 得到:nil(使用 == 比较) 哦,对不起,我现在编辑了我的答案。再试一次。我不小心在帖子之前放置了期望分配:创建,参数,它应该在它之后进行以上是关于尝试从acts_as_taggable_on 构建正确的夹具/工厂的主要内容,如果未能解决你的问题,请参考以下文章
Rails 4:acts_as_taggable_on 的 text_field 不使用逗号分隔标签
ruby acts_as_taggable_on + simple_form + select2