返回令牌或随机值rspec的测试方法

时间:2014-01-27 01:20:23

标签: ruby rspec

我有这个方法:

  def encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

我想写一个测试。但我应该测试什么?我唯一知道的是它不应该返回零值。但这可能是误报,不是吗?

测试encrypt实际加密的更好方法是什么?

  describe 'encrypt' do
    it "it returns an ecrypted token" do
      expect(subject.encrypt("hello")).not_to eq nil
    end
  end

2 个答案:

答案 0 :(得分:2)

您希望使用已知结果测试预期行为。寻找一个我遇到的例子 http://en.wikipedia.org/wiki/SHA-1#Example_hashes

describe 'encrypt' do
  it "it returns an ecrypted token" do
    text = 'The quick brown fox jumps over the lazy dog'
    hex = '2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12'
    expect(subject.encrypt(text)).to eq hex
  end
end

答案 1 :(得分:1)

SHA1哈希值通常为40个字符。您可以测试返回字符串的大小:

describe 'encrypt' do
  it "it returns an ecrypted token" do
    subject.encrypt("hello").size.should eq(40)
  end
end
相关问题