有没有办法在单个Ruby文件上运行Rspec测试?

时间:2019-05-27 17:36:47

标签: ruby rspec

我想对我正在从事的某些代码kata练习进行一些测试。是否可以在单个Ruby文件上运行Rspec测试?我尝试将require 'rspec'添加到文件顶部,然后从项目目录添加命令rsepc,但是返回以下内容:

F

Failures:

  1) Sentence reverser reverses the words in a sentence
     Failure/Error: expect(sentence_reverser(test_sentence)).to eq('I am backwards')

     NoMethodError:
       undefined method `sentence_reverser' for #<RSpec::ExampleGroups::SentenceReverser:0x0000559764dc5950>

我的代码是:

require 'rspec'

def sentence_reverser str
  str.split.reverse.join(' ')
end

describe "Sentence reverser" do
  it 'reverses the words in a sentence' do
    test_sentence = "backwards am I"
    expect(sentence_reverser(test_sentence)).to eq('I am backwards')
  end
end

1 个答案:

答案 0 :(得分:1)

尝试运行rspec </path/to/kata.rb>。那你甚至不应该require 'rspec'-只是测试了你的例子。