简体   繁体   English

Rspec>测试数据库视图

[英]Rspec > testing database views

How can database views be tested in Rspec? 如何在Rspec中测试数据库视图? Every scenario is wrapped in a transaction and the data does not look like it is being persisted to the database (MySQL in my case). 每个场景都包含在一个事务中,并且数据看起来不像是持久存储到数据库(在我的情况下是MySQL)。 My view returns with an empty result set because none of the records are being persisted in the transaction. 我的视图以空结果集返回,因为事务中没有任何记录持久存在。 I am validating that the records are not being stored by setting a debug point in my spec and checking my data with a database client while the spec is being debugged. 我正在验证通过在我的规范中设置调试点并在调试规范时使用数据库客户端检查我的数据来存储记录。

The only way I can think to have my view work would be if I could commit the transaction before the end of the scenario and then clear the database after the scenario is complete. 我认为让我的视图工作的唯一方法是,如果我可以在场景结束之前提交事务,然后在场景完成后清除数据库。 Does anyone know how to accomplish this or is there a better way? 有谁知道如何实现这一目标还是有更好的方法?

Thanks 谢谢

I think I got it. 我想我明白了。 In order to not use transactions, you need to specify: 为了不使用事务,您需要指定:

self.use_transactional_fixtures = false

You also must be sure to clean up what you create after each scenario. 您还必须确保在每个方案之后清理您创建的内容。

describe Attendee do
  self.use_transactional_fixtures = false

  def clear_all
    ActiveRecord::Base.connection.execute('delete from users')
    ActiveRecord::Base.connection.execute('delete from contact_info')
    ActiveRecord::Base.connection.execute('delete from events')
  end

  before(:each) do
    # create some test users
    @event = Factory.create(:event)
    @event.publish!
    @user = Factory.create(:user)
    @user_2 = Factory.create(:user_2)
  end

  after(:each) do
    clear_all
  end

  it "should list have attendees in the directory" do
    # in my case, Attendee class uses my attendees database view
    Attendee.count.should be 2
  end
end

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

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