“特征未注册”错误
随着Ben Walker的(令人惊叹的)Let's Build Instagram With Rails,特别是BDD版本。 教程使用FactoryGirl。 我在测试中遇到以下错误:
简洁版本
Failure/Error: post = create( :post, user_id = user.id )<br>
ArgumentError:<br>
Trait not registered: 1
我无法让Ben甚至用我的repo克隆重新创建bug,并且我在Stack Overflow“特征未注册”问题中找不到任何东西。
这是我的第一个SO问题,所以如果我在这方面做错了什么,请告诉我。 先谢谢您的帮助!
代码选择:
投机/ factories.rb
FactoryGirl.define do
factory :post do
caption "nofilter"
image Rack::Test::UploadedFile.new(Rails.root + 'spec/files/images/coffee.jpg', 'image/jpg')
user_id 1
end
factory :user do
email 'fancyfrank@gmail.com'
user_name 'Arnie'
password 'illbeback'
id 1
end
end
规格/功能/ viewing_posts_spec.rb
require 'spec_helper'
feature 'viewing individual posts' do
background do
user = create :user
post = create( :post, user_id = user.id )
sign_in_with user
end
scenario 'can click and view a post' do
post = create(:post)
visit '/'
find(:xpath, "//a[contains(@href,'posts/2')]").click
expect(page.current_path).to eq(post_path(post))
end
end
应用程序/模型/ post.rb
class Post < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x" }
validates_attachment_content_type :image, :content_type => /Aimage/.*Z/
end
应用程序/模型/ user.rb
class User < ActiveRecord::Base
validates :user_name, presence: true, length: { minimum: 4, maximum: 16 }
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts, dependent: :destroy
end
应用程序/控制器/ posts_controller.rb
class PostsController < ApplicationController
before_action :authenticate_user!
def index
@posts = Post.all
end
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = 'Your post has been created.'
redirect_to posts_path
else
flash[:alert] = 'Halt, you fiend! You need an image to post here!'
render :new
end
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
flash[:success] = 'Post updated hombre.'
redirect_to root_path
else
flash[:alert] = 'Something is wrong with your form!'
redirect_to root_path
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
flash[:success] = 'Problem solved! Post deleted.'
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:caption, :image)
end
end
完全错误 (其中之一)
viewing individual posts can click and view a post
Failure/Error: post = create( :post, user_id = user.id )
ArgumentError:
Trait not registered: 1
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/registry.rb:24:in `find'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/decorator.rb:10:in `method_missing'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl.rb:98:in `trait_by_name'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:113:in `trait_by_name'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:109:in `block in additional_traits'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:109:in `map'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:109:in `additional_traits'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:135:in `block in aggregate_from_traits_and_self'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:128:in `tap'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:128:in `aggregate_from_traits_and_self'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition.rb:33:in `to_create'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/definition_hierarchy.rb:16:in `build_from_definition'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:124:in `build_hierarchy'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:87:in `compile'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:32:in `run'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/notifications.rb:166:in `instrument'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /var/lib/gems/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/features/viewing_posts_spec.rb:6:in `block (2 levels) in <top (required)>'
修改您的工厂以使用association
。
FactoryGirl.define do
factory :post do
caption "nofilter"
image Rack::Test::UploadedFile.new(Rails.root + 'spec/files/images/coffee.jpg', 'image/jpg')
association user
end
factory :user do
email 'fancyfrank@gmail.com'
user_name 'Arnie'
password 'illbeback'
sequence(:id) { |id| id }
end
end
然后,创建用户并像这样发布:
user = create :user
post = create(:post, user: user)
这应该工作。
查看更多关于factory_girl关联的信息。
我也得到了这个完全相同的错误信息,这是因为我的spec文件中有这些代码行。
# .spec file
let!(:user) { create(:user) }
let!(:board) { create(:board, 1, user_id: user.id) }
let!(:categories) { create_list(:category, 5, board_id: board.id) }
在我的应用程序中,用户有很多电路板,电路板有很多类别。 在这部分规范中,我试图创建一个用户,一个董事会,通过id
属性链接到该用户,以及5个类别的列表,每个类别通过category
上的id
属性链接到该董事会。
问题是我复制了create_list
函数的参数来创建categories
并将其作为用于创建一个板子的create
函数的参数进行粘贴。
create
函数期望一个属性覆盖作为第二个参数,而create_list
函数需要一个整数,表示create_list
多少个对象来填充列表。
因为create
函数中用于单个category
的第二个参数1
代表'属性重写,因为这就是create
函数期望的第二个参数。 通过删除create
的第二个参数1,错误消失了,因为提供了正确的参数。
干杯。
链接地址: http://www.djcxy.com/p/87585.html