3

以下是我的 RSpec 代码片段:

describe UsersController do
  def mock_authentication_token(user, token_string)
    ...
    ...
  end

  def create_data
    @date_format = '%Y-%m-%d %H:%M:%S'
    today = Time.now
    @today_str = today.strftime(@date_format)
    ...
    ..
    ..
  end

  before do
    @current_user = Factory(:client)
    authtoken_str = "client auth token string"
    mock_authentication_token(@current_user, authtoken_str)
  end

  context "action: index" do
    before do
      create_data
      @params = @params.merge(limit: 5)
    end

    it "should return the more link with date set to 1 second ahead of #{@today_str}" do
      get :index, @params

      body = JSON.parse response.body

      ...
      ...
      ...
    end
end

此示例“应返回日期设置为比 #{@today_str} 提前 1 秒的更多链接”失败时它不会在失败的示例描述中打印由帮助方法create_data设置的实例变量 @today_str 的值。

它只是打印:应该返回日期设置为提前 1 秒的更多链接

似乎it方法不允许字符串插值。真的是这样吗?如果是,我如何实现所需的行为。

谢谢,吉涅什

4

1 回答 1

1

Rspec 在每个块之后重置类实例@变量。it

例如:

describe 'it blocks' do

  before :all
    @reset = 0
    @@global = 'will break tests'
  end

  it 'should increment' do
    @reset += 1
  end

  it "shouldn't forget it, either" do
    # but it does
    @reset.should eql 0
  end

  it 'does remember class-level variables, though' do
    @@global += ' for sure'
  end

  it 'can be demonstrated via' do
    @@global.split(' ').should > 3
  end

  # this is not the same @reset as what's in `before :all`.
  this_is_blank = @reset
  it "won't interpolate #{this_is_blank} because it's an instance level variable" do
    true.should be true
  end

  local = 'might as well hard code them into your descriptions at this point'
  it "Doesn't matter anymore because you #{local}" do
    true.should eql true
  end

  it "won't get here because class-level variables #{@@global}" do
    (2 + 2).should eql 5
  end

end

看起来你必须更通用地命名你的规范测试。反正我有

于 2013-09-26T18:24:39.257 回答