ruby - rake rspec 测试未运行

标签 ruby rspec rake

我按照 rspec 页面中的介绍,然后向我的 rakefile 添加了一个测试任务来进行简单的文件读取测试:

#Rakefile
task default: %w[test]

task :test do
  ruby "spec/file_reader_spec.rb"
end

#spec/file_reader_spec.rb
require './lib/filereader'
require 'rspec'

RSpec.describe "file_reader" do
    context "with sample test input file" do

        it "reads a file and prints its contents" do
            @file_reader = FileReader.new
            expect(@file_reader.input_file('./files/test_input.txt')).to eq ('file text')
        end
    end
end

但是当我运行 rake 命令时,它没有输出任何内容,只有一行显示规范文件已被执行:

$rake
/Users/mrisoli/.rvm/rubies/ruby-2.1.1/bin/ruby spec/file_reader_spec.rb

为什么它不输出所描述的测试?

最佳答案

您正在使用 ruby 运行规范,而不是 rspec。这就是为什么您看不到任何输出,您的测试将像常规 ruby​​ 脚本一样运行。

更改您的 Rakefile 以运行 rspec:

begin
  require 'rspec/core/rake_task'

  RSpec::Core::RakeTask.new(:spec)

  task :default => :spec
rescue LoadError
  puts "RSpec is not installed!"
end

更多详情here .

更新

如果你想向rspec传递参数,可以这样做:

RSpec::Core::RakeTask.new(:spec) do |t|
  t.rspec_opts = "--format documentation"
end

这将以文档格式运行规范。


偏离主题

当您描述一个类时,最佳实践表明您应该将类​​传递给 describe 方法而不是字符串。这样您的代码看起来更干净,并且 rspec 将自行实例化它(该实例将作为 subject 提供)。

例如:

RSpec.describe FileReader do
  context "with sample test input file" do
    it "reads a file and prints its contents" do
        expect(subject.input_file('./files/test_input.txt')).to eq ('file text')
    end
  end
end

关于ruby - rake rspec 测试未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973661/

相关文章:

ruby - 在 RSpec 中指定对象不接收任何消息

c# - 从 .NET Windows 服务运行 Rake

ruby-on-rails - 如何使用 Clockwork Rails 调度程序 Gem?

ruby - 我如何让 irb 从我运行 irb 的目录中获取 config/boot.rb 文件?

ruby-on-rails - Rails 4 - 使用带有 select_tag 的 Acts as Taggable gem 从集合中选择标签

ruby - 我如何从 ruby​​ 脚本中判断它是从命令行还是从非交互式进程运行的?

ruby - 检测由 RSpec、Ruby 运行的代码

ruby-on-rails - rails : undefined method Error when scoping

ruby-on-rails - 手动重新安装(没有自制程序)时,RSPEC找不到 Elasticsearch

ruby - 使用 :sql 模式格式时如何使 rake db :migrate generate schema. rb