简体   繁体   English

RSpec - 如何测试数据库视图?

[英]RSpec - How to test a database view?

I have a class that is backing a database view. 我有一个支持数据库视图的类。 I am using FactoryGirl to try and test this so I go through the normal steps of creating a user instance like so: 我正在使用FactoryGirl来测试这个,所以我按照正常步骤创建一个user实例,如下所示:

before(:each) do
  @user = FactoryGirl.create(:user)
end

Then I check the view count: 然后我检查视图计数:

Balance.all.count   # It is returning 0, it should be returning 1

My understanding of database views is that they do not update until after the transaction is committed to the database. 我对数据库视图的理​​解是,在事务提交到数据库之后才会更新。 I have a background working with NHibernate and in order to get an updated value from the database, I would do something like: 我有一个使用NHibernate的背景,为了从数据库中获取更新的值,我会做类似的事情:

Session.Flush();

That would allow me then to query the database view and get updated values but still rollback or commit my transaction later. 这样我就可以查询数据库视图并获取更新的值,但是后来仍然会回滚或提交我的事务。

Is there a way to do this in ActiveRecord? 有没有办法在ActiveRecord中执行此操作? Or is there a way to commit my changes to the database and clean it up? 或者有没有办法将我的更改提交到数据库并清理它? I don't want to turn off the transactions for all tests, just be able to test this one particular class. 我不想关闭所有测试的事务,只能测试这个特定的类。

let!(:user) { FactoryGirl.create(:user) }

您可能还想查看database_cleaner gem。

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

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