Tag: factory bot

如何在factory_girl中构建/创建多对多关联?

我有一个与Email模型有多对多关系的Person模型,我想创建一个工厂,让我为该人生成名字和姓氏(已经完成),并创建一个电子邮件地址,基于那个人的名字。 以下是我创建一个person姓名的方法: Factory.sequence :first_name do |n| first_name = %w[FirstName1 FirstName2] # … etc (I’m using a real subset of first names) first_name[(rand * first_name.length)] end Factory.sequence :last_name do |n| last_name = %w[LastName1 LastName2] # … etc (I’m using a real subset of last names) last_name[(rand * last_name.length)] end Factory.define :person do |p| #p.id ??? p.first_name […]

有序列化的领域的工厂女孩

我和工厂女孩有这个问题,它给了我一个undefined method ‘each’ for #错误,来自工厂的序列化字段。 在ActiveRecord中,它运行每个没有问题,因为返回的对象是一个数组。 我的问题是:如何在工厂中格式化序列化对象? 活动记录返回的方式? 或者它实际存储在数据库中的方式? (即序列化或不序列?)rspec会在保存和检索该活动记录时执行相同的序列化魔术吗? 这是我正在做的简化版本: Tvdb.rb–模型 class Tvdb ‘episodes_’ + info.id.to_s, :cache=>request) return request end end 然后在我的Series.rb模型中,我可以这样做: class Series < ActiveRecord::Base def episodes episodes = Tvdb.episodes(self.tvdb_id) episodes.each do |episode| puts episode.name end end end Tvdb.rb – 工厂 FactoryGirl.define do factory :series1_episodes, :class=>Tvdb do term ‘episodes_79488’ cache %q([#,# ) end […]

RSpec找不到Factorygirl的Factorys

我将在我的Rails3项目中使用RSpec和Factory girl。 我已安装工厂女孩,但它没有找到factorys我有这个错误 Failure/Error: Factory.build(:user).should_be valid No such factory: user spec / factories / user_factory.rb: Factory.define :user do |u| u.username ‘otto’ end 投机/ spec_helper.rb ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ‘factory_girl’ Dir[Rails.root.join(“spec/support/**/*.rb”)].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec config.fixture_path = “#{::Rails.root}/spec/fixtures” config.use_transactional_fixtures = true end 的Gemfile: group :development, :test do […]

Factory Girl + Rspec给出以下错误:ActionView :: Template :: Error :(对象id)不是id值

我定义了一个课程>>级别>>和工厂女孩的步骤。 Step属于level和level属于course(其中每个都有另一种has_many关系)。 这是错误(我甚至不知道从哪里开始): Failure/Error: before { visit course_level_step_path(course.id, level.id, step.id)} ActionView::Template::Error: 0x003fe5daf528a0 is not id value 这是我要求的路线: course_level_step GET /courses/:course_id/levels/:level_id/steps/:id(.:format) steps#show 在这里我的rspec: describe “attempting a step” do let(:course) { FactoryGirl.create(:course)} let(:level) { FactoryGirl.create(:level)} let(:step) { FactoryGirl.create(:step)} before { sign_in user} describe “taking a course” do before { visit course_level_step_path(course.id, level.id, step.id)} <<< here is the […]

测试cancanfunction并获得MassAssignmentSecurity :: Error

我已经实现了cancan,并希望按照cancan wiki的推荐测试能力。 我试图复制“用户只能销毁他拥有的项目”。 规格/型号/ ability_spec.rb: require “cancan/matchers” require ‘spec_helper’ describe Ability do context “user is investigator” do it “user can only destroy projects which he owns” do user = FactoryGirl.create(:user) ability = Ability.new(user) ability.should be_able_to(:destroy, Project.new(:user => user)) end end end 但是我得到: ActiveModel::MassAssignmentSecurity::Error: Can’t mass-assign protected attributes: user 楷模: class User true end class Project […]

FactoryGirl创建了不完整的模型

假设我有一个城市模型,其中: class city field :full_name, type: String # San Francisco, CA, United States field :_id, type: String, overwrite: true, default: ->{ full_name } end 假设我在/spec/factories/cities.rb中定义了一个工厂: FactoryGirl.define do factory :city do full_name ‘San Francisco, CA, United States’ end end 在其中一个规范中运行以下代码: city_attrs = { full_name: ‘San Francisco, CA, United States’ } City.create! city_attrs => # FactoryGirl.create(:city) => […]

FactoryGirl未注册(只有一家工厂遇到此事)

我有三种模式:用户,网站和护照。 用户和站点彼此拥有并且属于彼此,而护照是具有几个额外列的连接模型。 我尝试了以下两种方法: 1。 FactoryGirl.define do factory :passports do association :site, factory: :site association :user, factory: :user access_token ‘mock_token’ end end 2。 FactoryGirl.define do factory :passports do access_token ‘mock_token’ end end 无论哪种方式,我都用它: let(:site) { FactoryGirl.create :site } let(:user) { FactoryGirl.create :user } let(:site_user) { FactoryGirl.create :site_user, site: site, user: user } let(:passport) { FactoryGirl.create :passport, […]

如何在工厂中使用’sequence’来避免使用’FactoryGirl.reload’?

必须使用FactoryGirl.reload可能会花费一些开销来运行所有测试(当存在许多测试时),并且它也被描述为反模式 。 如何继续使用唯一字段的排序,然后测试正确的值? 你可以在我的代码中看到…… 我必须调用FactoryGirl.reload以便在任何先前的测试已经运行的情况下,增量从1开始。 我必须声明:count => 1因为只有一个该值的实例。 规格/工厂/ clients.rb FactoryGirl.define do factory :client do name “Name” sequence(:email} { |n| “email#{n}@example.com” } sequence(:username) { |n| “username#{n}” } end end 规格/客户/ index.html.erb_spec.rb require ‘spec_helper’ describe “clients/index” do before do FactoryGirl.reload (1..5).each do FactoryGirl.create(:client) end @clients = Client.all end it “renders a list of clients” do render […]

Factory Girl has_many通过validation死锁

我有一个Listing模型,其中has_many :categories, through: :categories_listings和has_many :categories_listings 。 我正在用Factory Girl工厂测试它,看起来像这样: factory :listing do |f| f.sequence (:title) { |n| “Listing #{n}” } message {Faker::Lorem.paragraph} cards {[FactoryGirl.create(:card)]} categories {[FactoryGirl.create(:category)]} association :delivery_condition association :unit_of_measure quantity 1 unit_price 1 end 在我向模型添加以下validation之前,一切正常: validate :has_categories? def has_categories? if self.categories_listings.blank? errors.add :base, “You have to add at least one category” end end 现在每当我经营工厂时,我得到: ActiveRecord::RecordInvalid: […]

如何使用rspec和factory测试模型的方法

我是铁杆上的新手,我必须用’Rspec’,’shoulda’和’factory girl’gem为现有的rails应用程序编写测试。 我可以测试非特定的测试,例如validates_presence_of:与’sholda’匹配器的东西。 但我想测试模型中的方法。 我可以想象我需要做什么,但我不能写作。 这就是我所说的一个例子: . . . context ‘is editable if project is not started’ do setup do @brief=Factory(:brief) @started_project=Factory(:project_started, :brief => @brief) @brief_have_no_project=Factory(:brief) end specify “with editable brief” do @brief.brand_info = ‘bla bla bla’ #change brand info, this is impossible #i can’t compose this section :S end specify “with non-editable brief” do […]