Rspec 生成的规范失败 [英] Rspec generated spec failing

查看:41
本文介绍了Rspec 生成的规范失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将以下内容添加到我的 application.rb 文件中

I've added the following to my application.rb file

config.generators do |g|
    g.test_framework :rspec,

        :fixtures => true,
        :view_specs => false,
        :helper_specs => false,
        :routing_specs => false,
        :controller_specs => true,
        :request_specs => true

    g.fixture_replacement :factory_girl, :dir => "spec/factories"
end

然后我用

$rails g controller home index

这在spec/controllers/home_controller_spec.rb中生成了如下所示的规范

This generated the specification in spec/controllers/home_controller_spec.rb which looks like this

describe HomeController do

  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end

end

这是 rSpec 生成的默认代码.我访问时可以看到页面

This is the default code generated by rSpec. I can see the page when I visit

http://localhost:3000/home/index 

在我的浏览器中

然后当我运行

$bundle exec rspec

我收到此错误

/Applications/MAMP/htdocs/2012/myapp/spec/controllers/home_controller_spec.rb:3:in `<top (required)>': uninitialized constant HomeController (NameError)
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
from /Users/bobwood/.rvm/gems/ruby-1.9.3-p194@myapp/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'

这个错误是什么意思,我该如何解决?

What does this error mean and how to I fix it?

routes.rb 中有一行

routes.rb has one line in it

get "home/index"

推荐答案

此错误意味着您运行的规范文件不知道您的 HomeController 类 - 它不包括在内.

This error means that the spec file you run doesn't know about your HomeController class - it's not included.

我认为您选择了一种更困难的方式来在 Rails 中使用 RSpec.最简单的方法是使用 rspec-rails.我查看了我的控制器规范,他们做的第一件事就是包含 rspec_helper.rb,它来自 rspec-rails.我猜这个文件用于加载测试所需的所有 Rails 类.

I think you chose a more difficult way to use RSpec in Rails. The easiest way is to use rspec-rails. I looked into my controller specs and the first thing they do is to include rspec_helper.rb, which comes from rspec-rails. I guess this file is used to load all the Rails classes you need for your test.

要安装 rspec-rails,您只需将其添加到 Gemfile 并运行:

To install rspec-rails all you need to do is add it to your Gemfile and run:

rails 生成 rspec:install

这篇关于Rspec 生成的规范失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆