Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a RSpec test to compare a string with its expected substring?

Tags:

ruby

rspec

rspec2

I have a RSpec test like this:

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

Now, to avoid platform dependency, the expected message within the eql() should be a substring without the initial C:/blah/blah/bin/ part.

My expected message should be "knife environment from file MySampleEnvironment.json --config 'knife.rb'"

Which should match with the actual message returned from knife.chef_cmd_to_s method: "C:/blah/blah/bin/knife environment from file MySampleEnvironment.json --config 'knife.rb'"

How do I do that? What Rspec matcher is used for this?

like image 658
kaushikv Avatar asked Jul 15 '15 15:07

kaushikv


1 Answers

This is where you would use the include matcher (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
like image 54
Rob Wise Avatar answered Oct 15 '22 18:10

Rob Wise