为什么我的Ruby测试只返回一个单词而不是整个auth_name字符串?

时间:2015-09-27 10:40:46

标签: arrays ruby rspec

这是我的代码:

describe WebServer::Htaccess do
  let(:auth_name) { "This is the auth_name" }
end

describe '#auth_name' do
  it 'returns the AuthName string' do
    expect(htaccess_user.auth_name).to eq auth_name
  end
end

这是我的规范:

 Failure/Error: expect(htaccess_user.auth_name).to eq auth_name

   expected: "This is the auth_name"
        got: "\"This"

   (compared using ==)

我的Ruby代码没有返回整个字符串,我不知道为什么。以下是失败测试的错误消息:

 var request = {
   method: 'POST',
   url: 'http://example.com',
   headers: {
     'Accept': 'application/json'
   },
   data: { test: 'test' }
 }

 $http(request ).then(function(){...}, function(){...});

1 个答案:

答案 0 :(得分:1)

使用阵列切片

  

@htaccess_hash [@commands [0]] = @commands [1]

默认情况下,

String#split在空格上分割,因此当您指定@commands[1]时,您只分配一个数组元素(单词“This”)作为哈希值。您可以做的最简单的事情是将下标更改为Array#slice,以将所有剩余的元素分配为哈希值,例如:

@commands = line.split
@htaccess_hash[@commands[0]] = @commands[1..-1]