使用 rspec 自动测试 Rails 上的 ruby

发布于 2024-10-20 10:13:38 字数 1620 浏览 2 评论 0原文

我正在使用 ruby​​ on Rails 进行自动测试。当我跑步时,我已经通过了两次测试。 rspec 规格/;但是,当我尝试使用自动测试时,这是输出:

matt@matt-laptop:~/sample_app$ autotest
loading autotest/rails_rspec2
style: RailsRspec2
matt@matt-laptop:~/sample_app$

我没有得到有关测试结果的输出。同样的事情也适用于bundle exec autotest。我看到一篇推荐 autospec 的帖子,但 rspec2 已弃用该命令。我的 Gemfile 是

source 'http://rubygems.org'

gem 'rails', '3.0.5'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'

group :development do
  gem 'rspec-rails', '2.5.0'
  gem 'autotest','4.4.4'
end

group :test do
  gem 'rspec', '2.5.0'
  gem 'webrat', '0.7.1'
  gem 'autotest', '4.4.4'
  gem 'redgreen', '1.2.2'
end

我尝试将 .autotest 配置文件放入项目的根目录以及主目录中,但两者对输出都没有影响。我的 .autotest 文件看起来像这样

#!/bin/ruby
require 'autotest/timestamp'

module Autotest::GnomeNotify
  def self.notify title, msg, img
    system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"
  end

  Autotest.add_hook :ran_command do |at|
    image_root = "~/.autotest_images"
    results = [at.results].flatten.join("\n")
    results.gsub!(/\\e\[\d+m/,'')
    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/)
    full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i)
  if output
    if failures > 0
      notify "FAIL", "#{output}", "#{image_root}/fail.png"
    elsif pending > 0
      notify "Pending", "#{output}", "#{image_root}/pending.png"
    else
      notify "Pass", "#{output}", "#{image_root}/pass.png"
    end
  end
 end
end

我还检查了 libnotify-bin 是否已安装并正常运行。

I'm using autotest with ruby on rails. I have passing 2 passing tests when I run. rspec spec/; however, when I try to use autotest this is the output:

matt@matt-laptop:~/sample_app$ autotest
loading autotest/rails_rspec2
style: RailsRspec2
matt@matt-laptop:~/sample_app$

I get no output about the results of the tests. The same thing works with bundle exec autotest. I saw a post recommending autospec but that command is deprecated with rspec2. My Gemfile is

source 'http://rubygems.org'

gem 'rails', '3.0.5'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'

group :development do
  gem 'rspec-rails', '2.5.0'
  gem 'autotest','4.4.4'
end

group :test do
  gem 'rspec', '2.5.0'
  gem 'webrat', '0.7.1'
  gem 'autotest', '4.4.4'
  gem 'redgreen', '1.2.2'
end

I have tried putting the .autotest config file in the root directory of my project as well as the home directory and neither makes a difference on the output. My .autotest file looks like this

#!/bin/ruby
require 'autotest/timestamp'

module Autotest::GnomeNotify
  def self.notify title, msg, img
    system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"
  end

  Autotest.add_hook :ran_command do |at|
    image_root = "~/.autotest_images"
    results = [at.results].flatten.join("\n")
    results.gsub!(/\\e\[\d+m/,'')
    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/)
    full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i)
  if output
    if failures > 0
      notify "FAIL", "#{output}", "#{image_root}/fail.png"
    elsif pending > 0
      notify "Pending", "#{output}", "#{image_root}/pending.png"
    else
      notify "Pass", "#{output}", "#{image_root}/pass.png"
    end
  end
 end
end

I've also checked that libnotify-bin is installed and functioning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟柳画桥 2024-10-27 10:13:38

要从 rspec 获得详细结果,请在根项目文件夹中创建一个 .rspec 文件并写入:

--format documentation

如果可以的话,请允许我建议 watchr 而不是 autotest(也可以使用 spork)。非常容易设置并且非常有效。

看看

http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html?utm_source=feedburner&utm_medium=feed& ;utm_campaign=Feed%3A+RubyInside+%28Ruby+Inside%29

如果您愿意的话。

To get verbose results from rspec, create a .rspec file in your root project folder and write :

--format documentation

If i may, allow me to suggest watchr instead of autotest (with spork as well). Very easy to set up and very effective.

Take a look at

http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+RubyInside+%28Ruby+Inside%29

if you like.

夜雨飘雪 2024-10-27 10:13:38

我在自动测试方面也遇到过同样的问题。我不确定,但我相信它可能对依赖版本非常挑剔。我是一个 Rails 菜鸟,但由于我最近分享了您的麻烦,让我向您展示我是如何解决这个问题的:

我能够通过删除 .autotest 文件中的所有代码来让它工作并将其替换为:

require 'autotest/growl'
require 'autotest/fsevent'

需要明确的是,第二行仅适用于您使用 OSX 的情况。我认为您可以安全地删除 .autotest 文件中的所有“代码”,因为它与手动“红/绿”咆哮通知相关,一旦安装了“redgreen”gem,就不再需要它了。简而言之,您最终将得到一两行的 .autotest 文件。

以下是我通过 gem 安装的一些文件(有些文件,如 rspec-expectations,应该作为依赖项自动安装)。我认为以下文件应该与您的自动测试设置相关,这些版本是撰写此回复之前 10 分钟的最新版本。

  • 自动测试(4.4.6,4.3.2)
  • 自动测试-fsevent(0.2.5,0.2.2)
  • 自动测试-咆哮(0.2.9,0.2.4)
  • 自动测试-rails-pure(4.1.2,4.1.0)
  • 红绿( 1.2.2)
  • rspec-core(2.5.1,2.0.0.beta.18)
  • rspec-期望(2.5.0,2.0.0.beta.18)
  • rspec-mocks(2.5.0,2.0.0.beta。 18)
  • rspec-rails (2.5.0, 2.0.0.beta.18)
  • spork (0.8.4)
  • webrat (0.7.3)
  • ZenTest (4.5.0)

执行gem list命令查看您安装了哪些。更新版本和简化 .autotest 文件可能是一个简单的问题(如果您关心位置,我的主目录中有我的文件)。另外,不要忘记 autotest-fsevent 文件仅适用于 OSX。

PS 你可能仍然会遇到无法解释的自动测试错误,除非你在 spec/controllers/spec_helper.rb 文件的最后添加一些额外的代码:

# Webrat configuration
Webrat.configure do |config|
config.mode = :rails
end

I've had much the same problem with Autotest. I'm not certain, but I believe it may be very finicky about dependency versions. I'm a bit of a Rails noob, but since I so recently shared your troubles, let me show you what I did to fix the problem:

I was able to get it working by getting rid of all the code in the .autotest file and replacing it with:

require 'autotest/growl'
require 'autotest/fsevent'

To be clear, that second line should only apply if you're using OSX. I think you can safely get rid of all the "code" in your .autotest file because it relates to manual "red/green" growl notifications, which is no longer necessary once you have the "redgreen" gem installed. In short, you'll end up with a one or two line .autotest file.

Here are some files I installed via gem (some, like rspec-expectations, should be automatically installed as dependencies). The following files are the ones I believe should be relevant to your Autotest setup, the versions are the latest as of 10 minutes prior to writing this response.

  • autotest (4.4.6, 4.3.2)
  • autotest-fsevent (0.2.5, 0.2.2)
  • autotest-growl (0.2.9, 0.2.4)
  • autotest-rails-pure (4.1.2, 4.1.0)
  • redgreen (1.2.2)
  • rspec-core (2.5.1, 2.0.0.beta.18)
  • rspec-expectations (2.5.0, 2.0.0.beta.18)
  • rspec-mocks (2.5.0, 2.0.0.beta.18)
  • rspec-rails (2.5.0, 2.0.0.beta.18)
  • spork (0.8.4)
  • webrat (0.7.3)
  • ZenTest (4.5.0)

Do a gem list command to see which ones you have installed. It might be a simple matter of updating your versions and simplifying your .autotest file (I have mine in my home directory, if you care about location). Also, don't forget that the autotest-fsevent file only applies to OSX.

P.S. You'll probably STILL end up with unexplained autotest errors unless you put some additional code at the very end of your spec/controllers/spec_helper.rb file:

# Webrat configuration
Webrat.configure do |config|
config.mode = :rails
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文