简体   繁体   English

Rails 3 / RSpec:控制器测试-在我的测试中访问类函数将返回SQLite3 :: SQLException“无此表”错误

[英]Rails 3 / RSpec: Controller tests — access to a class function in my test returns an SQLite3::SQLException “no such table” error

I have a Page scaffold that I created through rails generate scaffold. 我有一个通过导轨创建的Page脚手架生成脚手架。 I ran rake db:migrate, and viewing the database through SQLite Database Browser reveals that it in fact exists. 我运行了db:migrate,然后通过SQLite数据库浏览器查看数据库,发现它实际上存在。 I can go to the rails console and run: 我可以转到rails控制台并运行:


    Page.all.each {|page| puts page.url}

When I run this, it in fact returns the right results (eg, the value of url for each record of Page. 当我运行它时,它实际上会返回正确的结果(例如,每个Page记录的url值)。

What I am trying to do is set up an rspec test in my pages controller test to see that each of the routes I've set up in config/routes.rb actually works. 我想做的是在我的页面控制器测试中设置一个rspec测试,以查看我在config / routes.rb中设置的每条路由都可以正常工作。 So I have a test defined in spec/controllers/pages_controller_spec.rb as follows: 所以我在spec / controllers / pages_controller_spec.rb中定义了一个测试,如下所示:


describe PagesController do

    Page.all.each do |page|
        describe "GET /#{page.url}" do
            it "should be able to get at page at #{page.url}" do
                get "/#{page.url}"
                response.should be_successful
            end
        end
    end

end

This test does not run. 此测试无法运行。 Instead, it generates the following message: 而是,它生成以下消息:


C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log': SQLite3::SQLException: no such table: pages: SELECT     "pages".* FROM       "pages" (ActiveRecord::StatementInvalid)

Does anyone know what is going on? 有人知道发生了什么吗? Or, in the alternative, does anyone know why this will work in my Rails Console, but not in my rspec tests? 或者,是否有人知道为什么这将在我的Rails Console中起作用,而不在我的rspec测试中起作用?

Thank you so much. 非常感谢。 I am relatively new to Rails, so still learning a lot! 我是Rails的新手,所以仍然学到很多东西!

运行rake db:test:prepare以确保在测试环境中设置了数据库模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM