未定义的局部变量或方法`clientid'for#

时间:2015-01-23 06:19:27

标签: ruby rspec rspec-rails

我是factory_girl gem的新手。我的Ruby gem有静态clientid,session&主机。

我的工厂代码就像

FactoryGirl.define  do
 factory :session do |f|
        f.clientid "clientid string"
        f.secret " secret string"
        f.host "host string"
    end
end

我的规范代码就像

describe '#new' do
    it 'works' do
      result = FactoryGirl.build(clientid, secret, host)
      expect(result).not_to be_nil
    end
end

我的spec_helper文件是

require 'rspec'
require 'factory_girl'
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'mydemogem'

我正在尝试为此创建一个工厂。但它给了我以下错误:

undefined local variable or method `clientid' for #<RSpec::ExampleGroups::

2 个答案:

答案 0 :(得分:1)

以下是一些解释:

您已定义

factory :session

包含此工厂的文件通常称为 sessions_factory.rb ,因此它用于生成会话。您可以使用以下内容构建一个:

FactoryGirl.build(:session)

将调用:

Session.new(clientid: 'clientid string', secret: 'secret string', host: 'host string')

您还可以修改以下某些属性:

FactoryGirl.build(:session, clientid: 'other id string')

现在,返回的对象将等于:

Session.new(clientid: 'other id string', secret: 'secret string', host: 'host string')

这是您使用对象工厂的方式。

答案 1 :(得分:0)

您的规范

result = FactoryGirl.build(clientid, secret, host)

但您没有定义clientidsecrethost。您无法访问未定义的变量