简体   繁体   中英

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:

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:

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? 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。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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