factory-bot

How do I define sequences in FactoryGirlRails?

痞子三分冷 提交于 2019-12-07 16:28:50
问题 Previously in Factory girl, we could define sequences like so: # /spec/factories.rb FactoryGirl.define do # this is the sequence in question: sequence(:random_token) { Digest::MD5.hexdigest(rand.to_s) } factory :story do sequence(:title) { |n| "My Cool Story##{n}" } # Call the sequence here: token { Factory.next(:random_token) } description { "#{title} description"} end end Now, when I try that approach - I get a deprecation warning telling me: WARNING: FactoryGirl::Sequence#next is

Building a json stub using FactoryGirl

谁说胖子不能爱 提交于 2019-12-07 09:51:30
问题 I am try to build a json using a factory, but when i try to build it's empty. Below is Factory class. require 'faker' FactoryGirl.define do factory :account do |f| f.name {Faker::Name.name} f.description {Faker::Name.description} end factory :accjson, class:Hash do "@type" "accountResource" "createdAt" "2014-08-07T14:31:58" "createdBy" "2" "disabled" "false" end end Below is how i am trying to build. hashed_response = FactoryGirl.build(:accjson) expect(account.to_json).to eq(hashed_response

Rails 4 Use Factory Girl factories from Engine

纵饮孤独 提交于 2019-12-07 06:27:22
问题 I've created a rails engine (full, not mountable) to provide models to a number of different rails apps. I use Factory Girl Rails to test this engine and the tests all run fine for the engine itself. I now want to be able to use these factories in other apps that include this engine. The dependencies for the Gemspec look like this: s.add_dependency "rails", "~> 4.0.3" s.add_dependency "mysql2", "~> 0.3.15" s.add_development_dependency "rspec-rails", "~> 3.0.0.beta" s.add_development

Using FactoryGirl without Rails, ActiveRecord or any database with RSpec

左心房为你撑大大i 提交于 2019-12-07 02:01:18
问题 I was wondering if anyone knows whether it's possible to use FactoryGirl without any of the aforementioned prerequisites. I would like to use it to generate on-the-fly test data when driving UI automation tests for both mobile and web, and even possibly API. I know I could create some custom helper classes/methods and use getters and setters, etc., but I thought it would be good to use this awesome little gem. I have searched quite extensively and also tried to set up a basic RSpec project (I

I can't seem to add “config.include FactoryGirl::Syntax::Methods” to my rspec config block in spec_helper.rb

你离开我真会死。 提交于 2019-12-06 20:52:27
问题 If I add: config.include FactoryGirl::Syntax::Methods under RSpec.configure do |config| and run rspec, I see this error: /Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `block in ': uninitialized constant FactoryGirl (NameError) my gemfile.lock can be seen in this pastebin my gemfile can be seen in this pastebin If I omit the Rspec.comfigure statment, my tests all run fine. I'd like to make use of the abbreviated syntax, but am not sure what I am doing wrong here. 回答1: You

RSpec gives error 'trait not registered: name'

六月ゝ 毕业季﹏ 提交于 2019-12-06 16:58:57
问题 I tried to test my Rails 3 application on Windows with RSpec. I've wrote tests and factories, but can't solve the issues which raise when I run RSpec on command line. Here is one of the test files: require 'spec_helper' describe "SignIns" do it "can sign in" do user = FactoryGirl.create(:user) visit new_user_session_path fill_in "login", with: user.username fill_in "password", with: user.password click_on "sign in" current_user.username.should == user.username end end And here's the factories

Disabling a FactoryGirl association within a trait

瘦欲@ 提交于 2019-12-06 12:04:08
In a Rails app, I'm using FactoryGirl to define a general factory plus several more specific traits. The general case and all but one of the traits have a particular association, but I'd like to define a trait where that association is not created/built. I can use an after callback to set the association's id to nil , but this doesn't stop the association record from being created in the first place. Is there a way in a trait definition to completely disable the creation/building of an association that has been defined for the factory the trait belongs to? For example: FactoryGirl.define do

RSpec: Include a custom helper module in spec_helper.rb

泄露秘密 提交于 2019-12-06 08:18:16
I am trying to include a custom helper module in all my feature tests. I have tried creating the module in spec_helper.rb, but I get the following error: uninitialized constant FeatureHelper (NameError) Here is my spec_helper.rb as it currently is: # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec' include Warden::Test::Helpers module FeautreHelper def login shop = create(:shop) user = create(:user) login_as

Factory Girl with serialized field

荒凉一梦 提交于 2019-12-06 06:51:08
I'm having this issue with factory girl where it gives me a undefined method 'each' for #<String:0x0000012915bc18> error with a serialized field coming from the factory. within ActiveRecord, it runs the each with no problem, as the object returned is an array. My question is: how should I format the serialized object in my factory? The way that active record returns it? or the way it's actually stored in the database? (i.e. serialized or not?) will rspec do the same serialize magic on saving and retrieving that active record does? this is a simplified version of what I'm doing: Tvdb.rb-- Model

Testing “Post create” with Rspec

拈花ヽ惹草 提交于 2019-12-06 00:35:14
问题 I am trying to test a "Post create" action with Rspec. The code is as follows: def valid_attributes { :zone => Flymgr::Zone.new(:countries => Flymgr::ZoneCountry.first, :name => 'USA', :description => 'USA Flight', :zipcodes => ''), :price => '100.00', :class => 'first', } end def valid_session {} end before(:each) do @request.env["devise.mapping"] = Devise.mappings[:admin] admin = FactoryGirl.create(:admin) sign_in admin end describe "POST create" do describe "with valid params" do it