【问题标题】:Rspec > testing database viewsRspec > 测试数据库视图
【发布时间】:2011-01-25 08:17:39
【问题描述】:

如何在 Rspec 中测试数据库视图?每个场景都包含在事务中,并且数据看起来不像被持久化到数据库(在我的例子中是 MySQL)。我的视图返回一个空结果集,因为事务中没有保留任何记录。我正在通过在规范中设置调试点并在调试规范时使用数据库客户端检查我的数据来验证记录没有被存储。

我认为让我的视图工作的唯一方法是,如果我可以在场景结束之前提交事务,然后在场景完成后清除数据库。有谁知道如何做到这一点或有更好的方法吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby database rspec


    【解决方案1】:

    我想我明白了。为了不使用事务,您需要指定:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-18
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多