Rspec命名空间has_many关系失败

时间:2015-11-02 01:45:38

标签: ruby-on-rails rspec

在我的应用程序中,一个组织有多个发票,组织没有任何名称空间,但发票有Ihub :: invoice,在我的组织模型中我有

  has_many :invoices, class_name: 'Ihub::Invoice'

在我的控制器中,我有这条线,在开发中工作正常:

  @invoices = Ihub::Invoice.where(organisation_id: current_organisation.id).page(params[:page])

我的rspec因此错误而失败:

 Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::StatementInvalid:
   PG::UndefinedTable: ERROR:  relation "invoices" does not exist
   LINE 5:                WHERE a.attrelid = '"invoices"'::regclass
                                             ^
   :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                        pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                   FROM pg_attribute a LEFT JOIN pg_attrdef d
                     ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                  WHERE a.attrelid = '"invoices"'::regclass
                    AND a.attnum > 0 AND NOT a.attisdropped
                  ORDER BY a.attnum

更新

已经尝试过这没有帮助。

   RAILS_ENV=test rake db:drop db:create db:schema:load

开发中的SQL查询

   Ihub::Invoice Load (0.2ms)  SELECT "ihub_invoices".* FROM "ihub_invoices" WHERE "ihub_invoices"."organisation_id" = $1  [["organisation_id", 89]]

rspec中的sql错误(不同的查询Ihub :: Invoice.first),但你可以看到命名空间丢失

         Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::StatementInvalid:
   PG::UndefinedTable: ERROR:  relation "invoices" does not exist
   LINE 1: SELECT  "invoices".* FROM "invoices"  ORDER BY "invoices"."i...
                                     ^
   : SELECT  "invoices".* FROM "invoices"  ORDER BY "invoices"."id" ASC LIMIT 1

1 个答案:

答案 0 :(得分:0)

如果它在开发中有效但不在测试中,则可能是您的测试数据库不包含该表。

尝试:

bundle exec rake db:migrate
bundle exec rake db:test:prepare