0

モデルのメソッドをテストするために rspec を使用しています。すべて順調に進んでいますが、突然 factorygirl ビルドが機能しなくなりました。ここに私のコードがあります:

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

他のすべての @user は正しく設定され、機能していますが、ここに降りると:

「指定された年に支払われた請求書の正しい数を返す必要があります」 do user = @user debugger end

factorygirl.build は機能しません。特定のテストに直接含めて全員を入れようとしましたが、@userに設定されません。私は何が欠けていますか?これはエラーです:

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

@user が nil であるため、ユーザーを見ようとすると発生します

4

1 に答える 1

4

ユーザーが実際に何かをテストしようとしない限り、ユーザーはテストケースに実際には存在しないことがわかります...これは非常に奇妙です。

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

これにはユーザーが存在しますが、これは:

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

デバッガーが user = nil here を示した後にコンソールを見る。

于 2013-04-14T19:40:42.773 に答える