如何编写RSpec测试来比较字符串与其预期的子字符串?

时间:2015-07-15 15:15:59

标签: ruby rspec rspec2

我有一个像这样的RSpec测试:

context 'test #EnvironmentFile= method' do

  it 'compares the command generated with the expected command' do

    knife = EnvironmentFromFile.new(@knife_cfg_file)

    knife.environment_file=(@environment_file)

    expect(knife.chef_cmd_to_s).to eql("C:/blah/blah/bin/knife environment from file MySampleEnvironment.json --config 'knife.rb'")

  end
end

现在,为避免平台依赖,eql()中的预期消息应该是没有初始C:/ blah / blah / bin / part的子字符串。

我的预期信息应该是来自文件MySampleEnvironment.json的刀环境--config' knife.rb'"

哪个应该与knife.chef_cmd_to_s方法返回的实际消息匹配: " C:/ blah / blah / bin / knife环境来自文件MySampleEnvironment.json --config' knife.rb'"

我该怎么做? Rspec匹配器用于什么?

1 个答案:

答案 0 :(得分:6)

您可以在此处使用include匹配器(documentation)。

context 'test #EnvironmentFile= method' do

  it 'compares the command generated with the expected command' do

    knife = EnvironmentFromFile.new(@knife_cfg_file)

    knife.environment_file=(@environment_file)

    expect(knife.chef_cmd_to_s).to include("knife environment from file MySampleEnvironment.json --config 'knife.rb'")

  end
end