我按照 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/