Rails/RSpec "it"方法说明和字符串插值



以下是我的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

这个例子它"应该返回日期设置为提前1秒的链接#{@today_str}"做当失败时,它不打印实例变量@today_str的值由助手方法create_data设置,在失败的例子描述。

应该返回日期设置为比 早1秒的more链接

似乎 It 方法不允许字符串插值。这是真的吗?如果是,我该如何实现我想要的行为

谢谢,Jignesh

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

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

相关内容

  • 没有找到相关文章

最新更新