Topic: FactoryGirl

i am trying to upgrade my test to use FactoryGirl but they are not passing

my old test looks like

require 'spec_helper'
describe User do
  before do
    @user = User.new( email: "moisesz@email.com", password: "foobar", password_confirmation: "foobar", remember_me: true, username: "moiseszaragoza", first_name: "Moises", last_name: "Zaragoza", account_id: 1)
  end
   subject { @user }
   it { should respond_to(:email) }
   it { should respond_to(:password) }
   it { should respond_to(:password_confirmation) }
   it { should respond_to(:remember_me) }
   it { should respond_to(:username) }
   it { should respond_to(:first_name) }
   it { should respond_to(:last_name) }
   it { should respond_to(:account_id) }

 describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end
    end
  end


  describe "when First Name is too long" do
    before { subject.first_name = "a" * 21 }
   it { should_not be_valid }
  end
  describe "when Last Name is too long" do
    before { subject..last_name = "a" * 21 }
    it { should_not be_valid }
  end
  describe "when Username is too long" do
    before { subject..username = "a" * 21 }
    it { should_not be_valid }
  end
end

and this test works just perfect 

my new test

equire 'spec_helper'
describe User do

   subject { FactoryGirl.create(:user) }
   it { should respond_to(:email) }
   it { should respond_to(:password) }
   it { should respond_to(:password_confirmation) }
   it { should respond_to(:remember_me) }
   it { should respond_to(:username) }
   it { should respond_to(:first_name) }
   it { should respond_to(:last_name) }
   it { should respond_to(:account_id) }

 describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end
    end
  end


  describe "when First Name is too long" do
    before { User.first_name = "a" * 21 }
    it { should_not be_valid }
  end
  describe "when Last Name is too long" do
    before { @user.last_name = "a" * 21 }
    it { should_not be_valid }
  end
  describe "when Username is too long" do
    before { @user.username = "a" * 21 }
    it { should_not be_valid }
  end
end

i cant get it started

Re: FactoryGirl

Don't understand - can you provide the output from a run.

Re: FactoryGirl

OUTPUT


bundle exec rspec spec/models/user_spec.rb
FFFFFFFFFFFF

Failures:

  1) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ee8a1a8>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'

  2) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef25ab8>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>'

  3) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef2cea8>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:7:in `block (2 levels) in <top (required)>'

  4) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef340b8>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'

  5) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef3b4f8>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:9:in `block (2 levels) in <top (required)>'

  6) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef46718>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:10:in `block (2 levels) in <top (required)>'

  7) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef55b50>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:11:in `block (2 levels) in <top (required)>'

  8) User
     Failure/Error: subject { FactoryGirl.create(:user) }
     NoMethodError:
       undefined method `tbsacount=' for #<Account:0x007f878ef5cf68>
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:12:in `block (2 levels) in <top (required)>'

  9) User when email format is invalid should be invalid
     Failure/Error: @user.email = invalid_address
     NoMethodError:
       undefined method `email=' for nil:NilClass
     # ./spec/models/user_spec.rb:18:in `block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:17:in `each'
     # ./spec/models/user_spec.rb:17:in `block (3 levels) in <top (required)>'

  10) User when First Name is too long
     Failure/Error: before { User.first_name = "a" * 21 }
     NoMethodError:
       undefined method `first_name=' for #<Class:0x007f8789fa63c0>
     # ./spec/models/user_spec.rb:26:in `block (3 levels) in <top (required)>'

  11) User when Last Name is too long
     Failure/Error: before { @user.last_name = "a" * 21 }
     NoMethodError:
       undefined method `last_name=' for nil:NilClass
     # ./spec/models/user_spec.rb:30:in `block (3 levels) in <top (required)>'

  12) User when Username is too long
     Failure/Error: before { @user.username = "a" * 21 }
     NoMethodError:
       undefined method `username=' for nil:NilClass
     # ./spec/models/user_spec.rb:34:in `block (3 levels) in <top (required)>'

Finished in 0.05094 seconds
12 examples, 12 failures

Failed examples:

rspec ./spec/models/user_spec.rb:5 # User
rspec ./spec/models/user_spec.rb:6 # User
rspec ./spec/models/user_spec.rb:7 # User
rspec ./spec/models/user_spec.rb:8 # User
rspec ./spec/models/user_spec.rb:9 # User
rspec ./spec/models/user_spec.rb:10 # User
rspec ./spec/models/user_spec.rb:11 # User
rspec ./spec/models/user_spec.rb:12 # User
rspec ./spec/models/user_spec.rb:15 # User when email format is invalid should be invalid
rspec ./spec/models/user_spec.rb:27 # User when First Name is too long
rspec ./spec/models/user_spec.rb:31 # User when Last Name is too long
rspec ./spec/models/user_spec.rb:35 # User when Username is too long