如何测试JavaScript而不删除`test`数据库数据? [英] How to test JavaScripts without to delete the `test` database data?

查看:223
本文介绍了如何测试JavaScript而不删除`test`数据库数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ruby on Rails 3.2.2,cucumber-rails-1.3.0,rspec-rails-2.8.1和capybara-1.1.2。我想使用 Selenium 测试JavaScript,但不要删除 test 数据库数据每次我在我的终端窗口中运行 cucumber 命令行。也就是说,如果我声明一个像下面的功能:

 特性:... 

@ javascript
场景:...

JavaScript测试符合预期。但是,在测试运行后, test 数据库数据被删除,我必须重新生成该数据库才能正确运行新测试。



我阅读了官方文档以及 ROOT_APP / features / support / env.rb 文件(似乎我安装了所有需要的Ruby gems - 下面有关我使用的Gemfile的更多信息),但我不明白如何避免删除数据库数据以及如何配置Cucumber和Capybara宝石,以便正确使用Selenium。



我应该做什么? p>

注意我:我想做上述,因为我想有相同的 code>数据库数据,当我测试/运行方案



种子数据在 test 数据库(我的应用程序需要该数据工作),我在 RAILS_ROOT_PATH / lib / tasks / cucumber中添加以下代码。 rake 文件,并从终端窗口运行 rake db:test:prepare 命令行。

 命名空间:db do 
命名空间:test do
task:prepare => :environment do
Rake :: Task [db:seed]。invoke
end
end
end






ROOT_APP / features / support / env.rb 文件我试图取消注释以下两个代码块( BTW :我从来没有改变原始文件自动生成的 cucumber-rails gem,所以它是默认的),但在运行测试后,它仍然会删除测试数据库数据。

 #before('@ no-txn,@ selenium,@ culerity,@ celerity,@ javascript')do 
##{:except => [:widgets]}可能不会做你期望的这里
##作为tCucumber :: Rails :: Database.javascript_strategy重写
##这个设置。
#DatabaseCleaner.strategy =:truncation
#end

#Before('〜@ no-txn','〜@ selenium','〜@ culerity' 〜@ celerity','〜@ javascript')do
#DatabaseCleaner.strategy =:transaction
#end






Gemfile节选:

  :test do 
gemrspec-rails
end

group:test do
gem'cucumber-rails'
gem'database_cleaner'
gem'capybara'
end


解决方案

我遇到了同样的问题,并设法通过更改 ROOT_APP / features / support / env.rb

$下面的行修复它

来自

  Cucumber :: Rails :: Database.javascript_strategy =:truncation 

  Cucumber :: Rails :: Database.javascript_strategy =:transaction 

希望这有助于...


I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I would like to use Selenium in order to test JavaScripts, but without to delete the test database data each time I run the cucumber command line in my Terminal window. That is, if I state a feature like the following:

Feature: ...

  @javascript
  Scenario: ...

JavaScript is tested as well as expected. However, after the test has run, the test database data is deleted and I must seed again that database in order to properly run new tests.

I read the Official Documentation and the text present in the ROOT_APP/features/support/env.rb file (it seems that I installed all required Ruby gems - see below for more information about the Gemfile that I am using) but I didn't understand how to avoid to delete the database data and how to configure Cucumber and Capybara gems so to properly work with Selenium.

What should I make?

Note I: I would like to make the above because I would like to have the same test database data when I "test"/"run" Scenarios.

Note II: In order to seed data in the test database (my application needs that data to work), I add the following code in the RAILS_ROOT_PATH/lib/tasks/cucumber.rake file and I run the rake db:test:prepare command line from the Terminal window.

namespace :db do
  namespace :test do
    task :prepare => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end


In the ROOT_APP/features/support/env.rb file I tried to uncomment one and both of the following blocks of code (BTW: I never changed the original file auto-generated by the cucumber-rails gem, so it is the default one), but after running tests it still deletes the test database data.

#   Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
#     # { :except => [:widgets] } may not do what you expect here
#     # as tCucumber::Rails::Database.javascript_strategy overrides
#     # this setting.
#     DatabaseCleaner.strategy = :truncation
#   end
#
#   Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
#     DatabaseCleaner.strategy = :transaction
#   end


Gemfile excerpt:

group :development, :test do
  gem "rspec-rails"
end

group :test do
  gem 'cucumber-rails'
  gem 'database_cleaner'
  gem 'capybara'
end

解决方案

I ran into this same problem, and managed to fix it by changing the following line in ROOT_APP/features/support/env.rb

from

Cucumber::Rails::Database.javascript_strategy = :truncation

to

Cucumber::Rails::Database.javascript_strategy = :transaction

Hope this helps...

这篇关于如何测试JavaScript而不删除`test`数据库数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆