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

kau*_*ikv 4 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Rob*_*ise 6

这是您使用include匹配器(文档)的地方.

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
Run Code Online (Sandbox Code Playgroud)