Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation error on associated factory when using FactoryGirl with RSpec
    primarykey
    data
    text
    <p>I'm new to using RSpec and FactoryGirl. I'm trying to add RSpec tests to an existing codebase.</p> <p>I have the following factories defined:</p> <pre><code>Factory.sequence :email do |n| "somebody#{n}@example.com" end Factory.sequence :login do |n| "inquire#{n}" end Factory.define :user do |f| f.login { Factory.next(:login) } f.email { Factory.next(:email) } f.password 'inquire_pass' f.password_confirmation 'inquire_pass' f.first_name 'test' f.last_name 'guy' f.newsletter true f.notify_of_events true f.terms_of_service true end Factory.define :project do |project| project.title "Example Project Title" project.association :user project.association :provider project.association :project_request project.association :offering project.association :offering_type end </code></pre> <p>When I try to create a Project factory in my tests, however and assign it to @project:</p> <pre><code>require 'spec_helper' describe Charge do before(:each) do @provider_user = Factory(:user) @provider = stub_model(Provider, :user =&gt; @provider_user) @user = Factory(:user) @project_request = stub_model(ProjectRequest) @project = Factory(:project, :user =&gt; @user, :provider =&gt; @provider, :offering_fixed_fee_number =&gt; 700, :project_request_id =&gt; @project_request.id) @attr = { :user_id =&gt; @user.id, :provider_id =&gt; @provider.id, :charge_client =&gt; "0.01" } @charge = @project.build_charge(@attr) end </code></pre> <p>I get an error message when running the tests indicating that the validations for the associated user have failed:</p> <pre><code>Validation failed: User email can't be blank, User email is too short (minimum is 3 characters), User email does not look like a valid email address., Login can't be blank </code></pre> <p>The relevant validations on the User model are:</p> <pre><code> validates_presence_of :login, :email validates_uniqueness_of :login, :email, :case_sensitive =&gt; false validates_length_of :login, :within =&gt; 5..20 validates_format_of :login, :with =&gt; /^[a-z0-9-]+$/i, :message =&gt; 'may only contain letters, numbers or a hyphen.' validates_length_of :email, :within =&gt; 3..100 validates_format_of :email, :with =&gt; Authentication.email_regex, :message =&gt; 'does not look like a valid email address.' </code></pre> <p>I'm able to create valid User factories (by themselves) with no problem. But when I try to create projects that have a User association, and specify the associated User as the factory user I created earlier, the validations on that User fail. Any ideas what I'm missing here?</p> <p>Thanks very much,</p> <p>Dean Richardson</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload