URL Shortener上的在线Rails课程中的错误消息

时间:2011-12-16 12:45:18

标签: ruby-on-rails rspec

TryBloc有一个在线课程,可以在其他编程语言中教授Rails。有一个course on URL Shortening,我对第四课失败的rspec测试感到有点困惑(存储短代码)。

这应该是简单的,每一课都建立在之前的课程之上。我觉得好像我忽略了一些小东西,却无法发现它。 rspec的错误也没有帮助。我该如何解决这个问题并通过第四课?我正确地阅读了指令吗?

指示:

  

在您的第一次挑战中,我们要求您只返回一个5位数的随机字符串。

     

CHALLENGE将您生成的代码设置为Redis中的密钥,以便我们记住域的相同密钥。

给出的例子:

REDIS.set("12345", "google.com")
REDIS.get("12345") # google.com
REDIS.exists("12345") # true, the key exists

可以修改的代码:

require 'sinatra'

configure do
  require 'redis'
  require 'uri'
  REDISTOGO_URL = ENV["REDISTOGO_URL"]
  uri = URI.parse(REDISTOGO_URL)
  REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end

get "/" do
  "Try going to /shorten?url=http://www.google.com"
end

# yourapp.com/?url=http://google.com
get "/shorten" do
  # Write your code below to create a random number.
  random_number = (1..9).to_a.shuffle.sample(5).join("")

  REDIS.set(random_number, params[:url])
end

# Please leave this extra space at the bottom

错误:

  

˚F..

     

故障:

     

1)URL Shortener返回一个短代码

Failure/Error: last_response.body.should =~ /(\d){5}/

  expected: /(\d){5}/

       got: "http://google.com" (using =~)

  Diff:

  @@ -1,2 +1,2 @@

  -/(\d){5}/

  +http://google.com

# ./spec:42:in `block (2 levels) in <top (required)>'
     

以0.4169秒结束

     

3个例子,1个失败

     

失败的例子:

     

rspec ./spec:40#URL Shortener返回一个短代码

1 个答案:

答案 0 :(得分:1)

看起来RSpec希望"/shorten"返回一个5位数的代码,这在代码之间不会重复。

# yourapp.com/?url=http://google.com
get "/shorten" do
  # Write your code below to create a random number.
  random_number = (1..9).to_a.shuffle.sample(5).join("")

  # make sure that we don't re-use any numbers
  while REDIS.exists(random_number)
     random_number = (1..9).to_a.shuffle.sample(5).join("")
  end

  REDIS.set(random_number, params[:url])

  # return the number
  random_number
end