Matthew Berman
Matthew Berman

Reputation: 8631

rspec before(:each) using factorygirl to create model not working

I'm using rspec to test my model methods. Everything is going fine but all of a sudden factorygirl build isn't working. here's my code:

require File.dirname(__FILE__) + '/../spec_helper'

describe User do

  describe 'creation' do

    before(:each) do
      @user = FactoryGirl.build(:user)
    end

    it 'is invalid without an email address'  do
      user = @user
      user.email = nil
      user.should_not be_valid
    end

    it 'is invalid without a password' do
      user = @user
      user.password = nil
      user.should_not be_valid
    end

    it 'is invalid with an invalid email'  do
      user = @user
      user.email = "email@invalid"
      user.should_not be_valid
    end

    it 'is valid with a valid email and password'  do
      user = @user
      user.should be_valid
    end
  end

  describe "method" do

    before(:each) do
      @user = FactoryGirl.build(:user)
    end

    context "first_name" do

      it "should return the user's first name" do
        user = @user
        user.first_name.should == "User"
      end

    end

    context "count_of_invoices_by_year" do

      # @todo not sure how to check these since .count doesn't work
      it "should return the correct count of all invoices in the specified year" do
        # there are two invoices in 2013
        # user = @user
        # user.count_of_invoices_by_year("2013", :total).should == 2
      end

      it "should return the correct count of paid invoices in the specified year" do
        user = @user
        debugger
      end

      it "should return the correct count of sent invoices in the specified year" do

      end

    end

    context "sum_of_invoices_by_year" do

      it "should return the sum of the grand_totals of each of the invoices in the specified year" do
        # user.sum_of_invoices_by_year("2013", :total).should ==
      end

      it "should return the sum of the grand_totals of each of the paid invoices in the specified year" do

      end

      it "should return the sum of the grand_totals of each of the sent invoices in the specified year" do

      end

    end

  end

end

all the other @user are set correctly and work but when i get down here:

it "should return the correct count of paid invoices in the specified year" do user = @user debugger end

the factorygirl.build just doesn't work. I tried putting it everyone including directly in the specific test...but it just won't set to @user. what am i missing? this is the error:

NameError Exception: undefined local variable or method `user' for #<RSpec::Core::Example:0x007fc9ec7d4890>

which happens when i try to look at user because @user is nil

Upvotes: 0

Views: 2498

Answers (1)

Matthew Berman
Matthew Berman

Reputation: 8631

Turns out user isn't actually present in a test case unless you actually try to test something with it...which is very weird.

  it "should return the user's first name" do
    user.first_name.should == "User"
  end

this has user present but this:

  it "should return the user's first name" do
    debugger
  end

looking at the console after debugger shows user = nil here.

Upvotes: 4

Related Questions