Ruby on rails URL缩短器在线Rails课程中的错误消息
有一门在线课程,教授Rails和其他编程语言。第四课(存储短代码)的rspec测试失败了,这让我有点困惑 这应该很简单,每节课都建立在前面的基础上。我觉得自己好像忽略了一些小东西,但却看不见。来自rspec的错误也没有帮助。我怎样才能解决这个问题并通过第四节课?我读的说明书正确吗 指示: 在您的第一个挑战中,我们要求您只返回一个随机的5位数字符串 挑战将您生成的代码设置为Redis中的密钥,以便我们记住域的同一密钥 举例如下:Ruby on rails URL缩短器在线Rails课程中的错误消息,ruby-on-rails,rspec,Ruby On Rails,Rspec,有一门在线课程,教授Rails和其他编程语言。第四课(存储短代码)的rspec测试失败了,这让我有点困惑 这应该很简单,每节课都建立在前面的基础上。我觉得自己好像忽略了一些小东西,但却看不见。来自rspec的错误也没有帮助。我怎样才能解决这个问题并通过第四节课?我读的说明书正确吗 指示: 在您的第一个挑战中,我们要求您只返回一个随机的5位数字符串 挑战将您生成的代码设置为Redis中的密钥,以便我们记住域的同一密钥 举例如下: REDIS.set("12345", "google.com") R
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)>'
Failure/Error:last_response.body.should=~/(\d){5}/
应为:/(\d){5}/
得到:http://google.com“(使用=~)
差异:
@@ -1,2 +1,2 @@
-/(\d){5}/
+http://google.com
#/规格:42:in‘分块(2层)in’
以0.4169秒完成
3例,1例失败
失败的示例:
rspec./spec:40#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)>'
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
我很难说哪种方法(如果有的话)不符合规范。但是,有一点很清楚:规范正在测试一段代码,并希望它返回5位代码。但是,您的代码正在返回
”http://google.com“
。作为一个实验,您可以尝试将“12345”
作为get”/shorten“
块中的最后一行。这样做,我得到:response\u 1.body.should\u not==response\u 2.body预期不:==“12345”得到:“12345”。这是因为RSpec试图确保“/shorten”
route在请求之间不返回相同的值。我猜REDIS的任务是确保代码不被重复使用?我将在下面发布一个建议的解决方案。+1看到了这一点,并用随机数结束了我的区块,因此返回并通过了测试。这就是我们所缺少的一切。但这也解决了本课的第5步。谢谢