如何在Rspec中测试数据库视图?每个场景都包含在一个事务中,并且数据看起来不像是持久存储到数据库(在我的情况下是MySQL)。我的视图以空结果集返回,因为事务中没有任何记录持久存在。我正在验证通过在我的规范中设置调试点并在调试规范时使用数据库客户端检查我的数据来存储记录。
我认为让我的视图工作的唯一方法是,如果我可以在场景结束之前提交事务,然后在场景完成后清除数据库。有谁知道如何实现这个目标还是有更好的方法?
由于
答案 0 :(得分:3)
我想我明白了。为了不使用事务,您需要指定:
self.use_transactional_fixtures = false
您还必须确保在每个方案后清理您创建的内容。
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