factory-bot

What are factory_girl transient attributes? Why would I use one?

我与影子孤独终老i 提交于 2020-01-01 02:00:46
问题 I read this from Thoughtbot but it's still confusing to me. This is their example: factory :user do transient do rockstar true upcased false end name { "John Doe#{" - Rockstar" if rockstar}" } email { "#{name.downcase}@example.com" } after(:create) do |user, evaluator| user.name.upcase! if evaluator.upcased end end create(:user, upcased: true).name #=> "JOHN DOE - ROCKSTAR" So, Is .upcased a real attribute on the model? What is the transient block really doing? Setting variables that can then

Problem with Factory_girl, association and after_initialize

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 01:14:10
问题 I have a Family class so defined: class Family < ActiveRecord::Base after_initialize :initialize_family belongs_to :user validates :user, :presence => true validates :name, :presence => true, :length => { :maximum => 30 }, :format => { :with => /\A[a-zA-Z0-9\-_\s\']+\z/i} def initialize_family if self.name.blank? && self.user self.name = "#{self.user.profile_full_name}'s Family" end end end In my factories.rb I have: Factory.define :family do |f| f.association :user, :factory => :user end In

Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory

社会主义新天地 提交于 2019-12-31 10:59:05
问题 Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing. Anyhow, I have a very basic model test, not even fully implemented and when I try and run rspec spec/models/admin_spec.rb . I get the following error in the Admin has a valid factory line (full code below) Admin has a valid factory Failure/Error: Factory.create(:admin).should be_valid NameError: uninitialized

Factory girl saving records in my development database

夙愿已清 提交于 2019-12-31 07:23:28
问题 I have a very strange problem and I don't know where I should look to find it. I am developing a rails 3 app using rspec and factory girl for testing. For some reason, whenever I run any rails commands (eg to rake the db, start the dev server etc) one factory user is created and stored in my development database. The worst part is, it always has the same email, which I am validating the uniqueness of in my app, so the commands won't run until I go in manually delete the record. I have looked

rspec + factory model testing nested attributes

最后都变了- 提交于 2019-12-30 11:53:08
问题 I have a rails4 app with rspec + factory_girl. I would like to test the validation that makes sure the product has at least one feature, competition, usecase and industry. The first 3 must belong to a product, but industry can exist on its own. I haven't tried to put industry in the test yet since I can't even make work the first 3. I tried the approach below in which I create a product factory that has product_feature, product_competition and product_usecase. For some reason it's not working

“Could not find a valid mapping for #<User …>” only on second and successive tests

…衆ロ難τιáo~ 提交于 2019-12-30 00:53:09
问题 I'm trying to write a request test that asserts that the proper links appear on the application layout depending in whether a user is logged in or out. FWIW, I'm using Devise for the authentication piece. Here's my spec: require 'spec_helper' require 'devise/test_helpers' describe "Layout Links" do context "the home page" do context "session controls" do context "for an authenticated user" do before do # I know these should all operate in isolation, but I # want to make sure the user is

Factory_Girl not generating unique records

走远了吗. 提交于 2019-12-25 12:54:47
问题 I'm converting my application over to use factories instead of fixtures with Factory_Girl_Rails. I have the following factory defined: factory :requirement do sequence(:reqTitle) {|t| "Test Requirement #{t}"} ignore do categoryName " " categoryAbbr " " end reqText "This is a test general requirement for the purpose of, um, testing things" status "Approved" factory :reqWithCat do category end factory :reqWithNamedCat do category {create(:category, catName: categoryName, catAbbr: categoryAbbr)}

factorygirl create model association NoMethodError: undefined method

♀尐吖头ヾ 提交于 2019-12-25 07:56:04
问题 When I try to run FactoryGirl.create(:job, :purchased) I get the following error. I have been battling this for a long time now and I believe I have a pluralization issue. Issue Models class Job < ActiveRecord::Base belongs_to :company belongs_to :category has_one :coupon has_many :payments end class Payment < ActiveRecord::Base belongs_to :job belongs_to :coupon end class Coupon < ActiveRecord::Base belongs_to :job end Factory FactoryGirl.define do factory :job do category company title {

setting up objects for model testing with factory_girl

我是研究僧i 提交于 2019-12-25 07:26:38
问题 I'd like to test the scopes below but don't know hot to setup test objects properly. I don't even know if I should create those in factory with some associations or using let/local vars. task.rb belongs_to :assigner, class_name: "User" belongs_to :executor, class_name: "User" scope :completed, -> { where.not(completed_at: nil) } scope :uncompleted, -> { where(completed_at: nil) } scope :alltasks, -> (u) { where('executor_id = ? OR assigner_id = ?', u.id, u.id) } scope :between, -> (assigner

Rails FactoryGirl Factory with optional model association

谁都会走 提交于 2019-12-25 07:09:10
问题 In accordance with rails 4 model association left join validate id I had to update my factories. I am trying to create a condition where a coupon can be created, but has a job id only assigned when it is executed i.e. : FactoryGirl.define do factory :coupon do code { rand(25**25) } percent_discount { rand(100**1) } start_at { Time.now } end_at { 30.day.from_now } trait :executed do |c| job c.executed_at { Time.now } end end end 回答1: You can use callbacks in factory. Here is an example of it :