2011-12-16 164 views
0

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

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

指令:

In your first challenge, we asked you to return only a random string of 5 digits.

CHALLENGE Set the code you generate as a key in Redis so that we remember the same key for a domain.

给出的例子:

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..

Failures:

1) URL Shortener returns a short code

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)>' 

Finished in 0.4169 seconds

3 examples, 1 failure

Failed examples:

rspec ./spec:40 # URL Shortener returns a short code

+0

我很难说出你的方法中哪些方法没有规范。但是,这很明显:规范正在测试一段代码,并期望它返回一个5位数的代码。不过,您的代码正在返回'“http://google.com”。作为一个实验,你可以尝试把'12345'`作为你的`get'/'shorten'`块的最后一行。 – maxenglander 2011-12-16 20:42:08

+0

这样做,我得到:response_1.body.should_not == response_2.body expected:==“12345” got:“12345”。 – spong 2011-12-16 21:08:29

回答

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