Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将RSpec配置为在单元测试中调用DB时引发错误_Ruby On Rails_Ruby_Rspec Rails_Rspec3 - Fatal编程技术网

Ruby on rails 将RSpec配置为在单元测试中调用DB时引发错误

Ruby on rails 将RSpec配置为在单元测试中调用DB时引发错误,ruby-on-rails,ruby,rspec-rails,rspec3,Ruby On Rails,Ruby,Rspec Rails,Rspec3,Snappy测试,特别是单元测试,应该编写,这是毫无疑问的。在标记为:unit的测试中检测到数据库命中时,如何配置RSpec以引发错误。这加强了编写快速测试的策略,并在不需要时避免数据库命中 现在,我已经有了这段代码,但不管怎样,它总是会引发错误,即使我在测试示例中放置了一个简单的puts Hello config.around(:each, :unit) do |example| callback = lambda { |name, start, finish, message_id, p

Snappy测试,特别是单元测试,应该编写,这是毫无疑问的。在标记为:unit的测试中检测到数据库命中时,如何配置RSpec以引发错误。这加强了编写快速测试的策略,并在不需要时避免数据库命中

现在,我已经有了这段代码,但不管怎样,它总是会引发错误,即使我在测试示例中放置了一个简单的puts Hello

config.around(:each, :unit) do |example|
  callback = lambda { |name, start, finish, message_id, payload|
    raise "Nope! Accessing the database in a unit test is not allowed!!\n\n Offending Query\n#{ payload[:sql] }"
  }
  ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
    example.run
  end
end
提高

Got 0 failures and 2 other errors:

     1.1) Failure/Error: raise "Cannot access the database in a unit test!\n\n Offending Query\n#{"="*20}\n#{ payload[:sql] }"

          RuntimeError:
            Cannot access the database in a unit test!

             Offending Query
            ====================
            BEGIN
          # ./spec/rails_helper.rb:54:in `block (3 levels) in <top (required)>'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/active_record/transaction.rb:12:in `start'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/base.rb:90:in `start'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:75:in `block in start'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:75:in `each'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:75:in `start'
          # ./spec/support/database_cleaner.rb:12:in `block (2 levels) in <top (required)>'
          # ./spec/rails_helper.rb:57:in `block (3 levels) in <top (required)>'
          # ./spec/rails_helper.rb:56:in `block (2 levels) in <top (required)>'

     1.2) Failure/Error: raise "Cannot access the database in a unit test!\n\n Offending Query\n#{"="*20}\n#{ payload[:sql] }"

          RuntimeError:
            Cannot access the database in a unit test!

             Offending Query
            ====================
            SELECT c.relname FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = ANY (current_schemas(false)) AND c.relkind IN ('r')
          # ./spec/rails_helper.rb:54:in `block (3 levels) in <top (required)>'
          # ./spec/support/database_cleaner.rb:19:in `block (2 levels) in <top (required)>'
          # ./spec/rails_helper.rb:57:in `block (3 levels) in <top (required)>'
          # ./spec/rails_helper.rb:56:in `block (2 levels) in <top (required)>'
Got 0 failures and 2 other errors:

     1.1) Failure/Error: raise "Cannot access the database in a unit test!\n\n Offending Query\n#{"="*20}\n#{ payload[:sql] }"

          RuntimeError:
            Cannot access the database in a unit test!

             Offending Query
            ====================
            BEGIN
          # ./spec/rails_helper.rb:54:in `block (3 levels) in <top (required)>'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/active_record/transaction.rb:12:in `start'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/base.rb:90:in `start'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:75:in `block in start'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:75:in `each'
          # /home/jedrek/.rvm/gems/ruby-2.4.0/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:75:in `start'
          # ./spec/support/database_cleaner.rb:12:in `block (2 levels) in <top (required)>'
          # ./spec/rails_helper.rb:57:in `block (3 levels) in <top (required)>'
          # ./spec/rails_helper.rb:56:in `block (2 levels) in <top (required)>'

     1.2) Failure/Error: raise "Cannot access the database in a unit test!\n\n Offending Query\n#{"="*20}\n#{ payload[:sql] }"

          RuntimeError:
            Cannot access the database in a unit test!

             Offending Query
            ====================
            SELECT c.relname FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = ANY (current_schemas(false)) AND c.relkind IN ('r')
          # ./spec/rails_helper.rb:54:in `block (3 levels) in <top (required)>'
          # ./spec/support/database_cleaner.rb:19:in `block (2 levels) in <top (required)>'
          # ./spec/rails_helper.rb:57:in `block (3 levels) in <top (required)>'
          # ./spec/rails_helper.rb:56:in `block (2 levels) in <top (required)>'