English 中文(简体)
RSpec无法加载我的非ActiveModel模型类
原标题:RSpec can t load my non ActiveModel model classes

我是在Rails应用程序中使用RSpec的新手,我无法让RSpec看到我的模型:(

我在我的Gemfile中添加了以下行

group :development, :test do
  gem  rspec-rails 
end

并安装了

rails generate rspec::install

然后我在app/models/目录中创建了一个facebook_page.rbActiveModel

class FacebookPage
  class << self
    # define class variables
    attr_accessor :app_id
    attr_accessor :app_secret
  end
end

现在我正在尝试测试它,所以我在spec/models/目录中添加了一个facebook_page_spec.rb

describe FacebookPage do
  describe ".app_id" do
    it "should return the FB_APP_ID environment variable and thus not be null" do
      FacebookPage.app_id.should_not be_empty 
    end
  end
end

但当我奔跑时

bundle exec rake spec

我得到以下错误:

/home/geoffroy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ./spec/models/facebook_page_spec.rb
/home/geoffroy/dev/mybandpage/spec/models/facebook_page_spec.rb:1:in `<top (required)> : uninitialized constant FacebookPage (NameError)

RSpec does not seem to load my model file. Is it because it s not a subclass of ActiveRecord ? How can I solve that ?

提前非常感谢,最好

杰弗里

最佳回答

您在rails generate rspec::install(双冒号)中有一个拼写错误,请尝试使用rails generate rspec:install。还可以尝试使用以下命令bundle-exec-rspec-spec运行测试。

您在测试中是否需要“spec_helper”

ps.此代码错误:

class FacebookPage
  class << self
    # define class variables
    attr_accessor :app_id
    attr_accessor :app_secret
  end
end

基本上,在这个例子中,您正在单例类上定义实例变量,请尝试使用这种方法:http://apidock.com/rails/Class/cattr_accessor一

问题回答

对我来说,它没有在规范的开头包含require-rails_helper





相关问题
How to use rspec to test named routes?

Given I have a named route: map.some_route /some_routes/:id , :controller => some , :action => other How do I use the routing spec file spec/routing/some_routing_spec.rb to test for that ...

Silencing Factory Girl logging

Just to clear the air, I am not some cruel factory master trying to silence working ladies. I am having a very annoying problem where when using Thoughtbot s factory girl in my specs, every time ...

Controller specs in isolation mode and render :update

I am using RSpec for writing my controller tests/specs. I faced the problem, that the following code gets rendered: render :update do |page| page[ middle_content ].replace_html :partial => "...

rspec testing views with internationalization?

I want to make sure I have the right meta desc/keyword and title text in my view so I want to create rspec views tests for that. Now the real challange here is how to make it work across multiple ...