factory-bot

How to build a parent with child factory in one step in order to pass validation

独自空忆成欢 提交于 2019-12-11 02:37:38
问题 Projects must have at least one task created at the same time to ensure the validation passes. This is the snippet I use to validate this: class Project < ActiveRecord::Base validates :tasks, :length => { :minimum => 1 } ... end The challenge I'm having is create the right factory to build a project with task upfront using FactoryGirl. I'm using: FactoryGirl.define do factory :task do name "Get this test passing" project end factory :project do title "Complete the application" factory

How to make a FactoryGirl.create affect another record's attribute?

大城市里の小女人 提交于 2019-12-11 01:19:50
问题 In a webshop application normally after a booking of a product the bookings_controller create action does an order.save which in turn activates the necessary before_save method order.sum_of_all_bookings . When building a RSpec test for an admin viewing a list of orders, making a booking doesn't change the total of an order. The indexing does work in the browser. require 'rails_helper' RSpec.feature 'Admin can oversee orders' do let(:admin) { FactoryGirl.create(:user, :admin) } let(:customer)

How do you define trait using Fabrication

五迷三道 提交于 2019-12-10 20:27:57
问题 I am learning how to use fabrication in Rails and we have decided to replace all our factory_girl code with fabrication. Suppose we have this code in factory_girl FactoryGirl.define do factory :user do trait(:no_credits) { credits 0 } trait(:with_credits) { credits 300 } How will you define this in Fabrication? I have gone through their website but couldn't find anything regarding this. Will appreciate your help 回答1: Seems that your are looking for Fabricator inheritance, this is what the

rspec test failure when switching from initializing attributes to Factory

穿精又带淫゛_ 提交于 2019-12-10 18:05:52
问题 This makes no sense to me, hope someone can help. I'm getting a single test failure when creating a user using a factory (seemingly similar tests pass). In my user_spec.rb test file, the user was originally created like this. Using this initialization approach, all tests pass. USER CREATION user_spec.rb (without factory) describe User do before do @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar", username: "username") end

When to switch from cucumber to rspec in the BDD cycle for a login procedure

丶灬走出姿态 提交于 2019-12-10 17:47:53
问题 I'm still trying to understand the combination of cucumber and rspec in the BDD cycle. I have defined the following scenarios for a very simple login system: Feature: Log in In order to get access to the application As a user I want to log in Scenario: User logs in successfully Given I exist as a user When I go to the login page And I fill in "username" with "gabrielhilal" And I fill in "password" with "secret" And I press "Login" Then I should see "Welcome gabrielhilal" And I should be

ERROR: null value in column “id” violates not-null constraint

。_饼干妹妹 提交于 2019-12-10 16:04:42
问题 I just migrated my app from mysql to postgres but when I try to insert a record in a specific table I get violates not-null constraint error: ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null). ********** Error ********** ERROR: null value in column "id" violates not-null constraint SQL state

Reset 'sequence' for FactoryGirl tests

こ雲淡風輕ζ 提交于 2019-12-10 15:59:17
问题 Does anyone know how to reset the sequence method for FactoryGirl? I have a factory that creates a list of tasks and I want to order to start at 1 every time. I use 'sequence' because the task list is a associated model, so I would need the order to increase every time I use FactoryGirl.create until I call a reset. 回答1: You need to write FactoryGirl.reload in the before/after callback of the test file. Example code snippet before do FactoryGirl.reload . . . end The '.' represents other code.

Rails FactoryGirl inside app in development env

天大地大妈咪最大 提交于 2019-12-10 15:54:00
问题 I'm trying to use FactoryGirl gem inside my App in development mode (it's for mailer tests more) with rails_email_preview gem. It works but only on initial page load, after reloading/refreshing the page I get following error: Factory not registered: order where order is name of factory. Here is my code (it is a dummy app for my Rails Engine): spec/dummy/app/mailer_previews/mymodule/order_mailer_preview.rb Dir[Mymodule::Core::Engine.root.join('spec', 'factories', '*')].each do |f| require

rspec integration test with devise throws NoMethodError error

瘦欲@ 提交于 2019-12-10 13:01:26
问题 I have a similar error posted here, but all will not fix my problem. My file 'spec/request/news_controller_spec.rb' looks like: require 'spec_helper' describe "NewsController" do include Devise::TestHelpers describe "GET /news" do before(:each) do @request.env["devise.mapping"] = Devise.mappings[:user] @user = Factory.create(:user) @user.confirm! sign_in @user end it "after login should show the latest news an screen" do page.should have_content("News") end end end If I run the test I'll get:

Should we be using Faker in Rails Factories?

拈花ヽ惹草 提交于 2019-12-10 12:53:17
问题 I love Faker, I use it in my seeds.rb all the time to populate my dev environment with real-ish looking data. I've also just started using Factory Girl which also saves a lot of time - but when i sleuth around the web for code examples I don't see much evidence of people combining the two. Q. Is there a good reason why people don't use faker in a factory? My feeling is that by doing so I'd increase the robustness of my tests by seeding random - but predictable - data each time, which