factory-bot

How to create factories with attr_accessible?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 06:58:29
问题 How to deal with factories and attr_accessible ? My example: # model class SomeModel attr_accessible :name, full_name, other_name end #spec require 'spec_helper' describe "test" do it do create(:some_model, name: "test name", user: User.first) #factory end end #error ruby(17122,0x12a055000) malloc: *** error for object 0x8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug I think the error is because user_id is not in attr_accessible attributes. 回答1:

Testing cancan abilities and getting MassAssignmentSecurity::Error

纵然是瞬间 提交于 2019-12-25 05:18:31
问题 I have implemented cancan and would like to test abilities as recommended on the cancan wiki. I trying to replicate "user can only destroy projects which he owns." spec/models/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 However I

How can I avoid using 'FactoryGirl.reload' due to using 'sequence' in my factories?

微笑、不失礼 提交于 2019-12-25 03:21:50
问题 Having to use FactoryGirl.reload likely to put some overhead on the time it takes to run all tests (when many tests exist) and also it is described as an Anti-Pattern. How can I keep using the sequencing for unique fields, yet test for the correct values? You can see in my code that... I have to call FactoryGirl.reload so that the increment starts at 1 in case any previous tests have already been run. I have to state :count => 1 because there will only ever be one instance of that value. spec

FactoryGirl creating extra records with has_many association

拜拜、爱过 提交于 2019-12-25 02:22:35
问题 I'm running into an issue where FactoryGirl appears to be creating extra records with a has_many relationship. Given these models: class NextAction < ActiveRecord::Base has_many :next_actions_orders has_many :orders, through: :next_actions_orders end class NextActionsOrder < ActiveRecord::Base belongs_to :order belongs_to :next_action end class Order < ActiveRecord::Base has_many :next_actions_orders has_many :next_actions, through: :next_actions_orders end And these factories: FactoryGirl

Create Parent and Child with child presence validation Factory Girl

落爺英雄遲暮 提交于 2019-12-24 15:12:33
问题 Have a project that has Invoices with many Trips. New story came my way requesting that an Invoice MUST have a trip. I've added a validation validates :trips, presence: true but it is now blowing up a number of my tests since FactoryGirl is trying to save the invoice before creating the associated trip. FactoryGirl.define do factory :invoice do sequence(:invoice_id) { SecureRandom.uuid} merchant amount 100.00 item_count 1 paid false currency "GBP" invoice_type "pre-flight" service_rendered

FactoryGirl: Building an object creates its associated object

…衆ロ難τιáo~ 提交于 2019-12-24 12:49:48
问题 I don't know if this is a bug with FactoryGirl or if it is something i am doing wrong I have two factory definitions factory :employee do name "name1" association :department end factory :department do name "department1" end I would expect the following to build both employee and department FactoryGirl.build(:employee, :name => "employee") But it builds the employee object and creates department in the database. I am sure it use to work in some older versions of FactoryGirl. I am using

find_or_initialize_by in FactoryGirl

那年仲夏 提交于 2019-12-24 11:27:52
问题 I was wondering if there's an equivalent for find_or_initialize_by in FactoryGirl that solve teh following issue: The objective is that the model uses two tables that have the same country. I don't want to use a sequence for the country (as I found for Emails). There's a uniqueness constraint on Country, but my main issue is that it create twice the same record of Country when I call once FactoryGirl.create(:click) Thus, the Validation fail in the test. Rspec: # models/click_spec.rb describe

ActionController::UrlGenerationError: missing required keys: [:id]

房东的猫 提交于 2019-12-24 10:35:36
问题 I'm running RSpec 3 tests and am getting this same error for this one particular path: Failure/Error: visit book_path ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"books"} missing required keys: [:id] My test isn't quite finished, so I'm sure some of the latter code might be incorrect. But I can't get my visit path line to run: ... book = FactoryGirl.build(:book) reviews = FactoryGirl.build_list(:review, 5, book: book) visit book_path reviews.each do

Authlogic and password and password confirmation attributes - inaccessible?

允我心安 提交于 2019-12-24 08:46:36
问题 Im trying to test my successfully creates a new user after login (using authlogic). Ive added a couple of new fields to the user so just want to make sure that the user is saved properly. The problem is despite creating a valid user factory, whenever i try to grab its attributes to post to the create method, password and password confirmation are being ommitted. I presuem this is a security method that authlogic performs in the background. This results in validations failing and the test

Why am I getting FactoryGirl wrong number of arguments error?

心已入冬 提交于 2019-12-24 07:17:58
问题 Having strange behavior from FactoryGirl in non-rails app. getting wrong number of arguments error...don't know why. gideon@thefonso ~/Sites/testing (master)$ rspec spec /Users/gideon/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:6:in `define': wrong number of arguments (1 for 0) (ArgumentError) from /Users/gideon/Sites/testing/spec/factories.rb:1:in `<top (required)>' Here are the files involved... login_user_spec.rb require_relative '../spec_helper'