0

我正在关注 Michael Hartl 关于 Ruby on Rails(rails 3.2 版本)的好书。我在第9.3.3 节中遇到了一些问题。分页

在我修改我的工厂后spec/factories.rb使用sequence这样的(请注意,我已将以前的版本注释掉):

FactoryGirl.define do
    #factory :user do
    #   name "Michael Hartl"
    #   email "michael@example.com"
    #   password "foobar"
    #   password_confirmation "foobar"
    #end
    factory :user do
        sequence(:user){ |n| "Person #{n}" }
        sequence(:email){ |n| "person_#{n}@example.com"}
        password "foobar"
        password_confirmation "foobar"
    end
end

我不能再在我的测试中使用它了:

let(:user){ FactoryGirl.create(:user) }

例如带有代码的测试:

require 'spec_helper'

describe "User pages" do

    subject { page }
    describe "index" do

        let(:user){ FactoryGirl.create(:user) }

        before(:all){ 30.times {FactoryGirl.create(:user) }}
        after(:all) {User.delete_all}

        before(:each) do
            valid_signin user
            visit users_path
        end

        it { should have_selector('title', text: 'All users') }
        it { should have_selector('h1',  text: 'All users') }

.
.
.
end

返回错误,例如:

Failure/Error: before(:all){ 30.times {FactoryGirl.create(:user) }}
     NoMethodError:
       undefined method `user=' for #<User:0x00000002760140>
     # ./spec/requests/user_pages_spec.rb:16:in `block (4 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:16:in `times'
     # ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

不知何故,我不能再使用这种语法 ( let(:user){...}),因为我现在正在创建元素序列,但我似乎找不到解决这个问题的方法。

有任何想法吗?谢谢!

4

1 回答 1

1

我认为你的意思是sequence(:name)而不是sequence(:user)在你的工厂。user=它正在您的模型中寻找方法User而不是name=方法。

于 2013-09-18T21:45:02.473 回答