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
我很難說出你的方法中哪些方法沒有規範。但是,這很明顯:規範正在測試一段代碼,並期望它返回一個5位數的代碼。不過,您的代碼正在返回'「http://google.com」。作爲一個實驗,你可以嘗試把'12345'`作爲你的`get'/'shorten'`塊的最後一行。 – maxenglander 2011-12-16 20:42:08
這樣做,我得到:response_1.body.should_not == response_2.body expected:==「12345」 got:「12345」。 – spong 2011-12-16 21:08:29