以下是我的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
此示例 it“应返回更多链接,日期设置为#{@ today_str}之前的1秒”执行失败时它不会打印实例变量@today_str设置的值失败的示例说明中的辅助方法 create_data 。
它只是打印:应该返回更多链接,日期设置为之前的1秒
似乎它方法不允许字符串插值。这是真的吗?如果是,我如何实现所需的行为。
谢谢, Jignesh
答案 0 :(得分: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
看起来你必须更一般地命名你的规范测试。无论如何,我有。