我怎样才能正确使用 rspec?
how can i use rspec correctly?
我的代码使用 github API 创建了一个 github 要点。创建要点后, API returns 给我一个状态码。如果代码是“201”,则创建了要点。但如果它是“400”,就会出错。在我的代码中,保存该状态的变量是 response_status
这是我的代码:
require 'net/http'
require 'json'
require 'uri'
class Gist
attr_reader :response_status
attr_reader :try_again
def initialize(filename,description,state,content)
@filename = filename
@description = description
@state = state
@content = content
end
def post(uri, request)
request.basic_auth("my_username", "my_token")
request.body = JSON.dump({
"description" => @description,
"public" => @state,
"files" => {
@filename => {
"content" => @content
}
}
})
req_options = { use_ssl: uri.scheme == "https" }
begin
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
json = response.body
parsed = JSON.parse(json)
@response_status = "#{ response.code }"
if @response_status == "201"
puts "Tu gist se ha creado con exito. La URL de su gist es: "+ parsed["url"]
end
rescue SocketError => se
puts "Ha ocurrido un error de conexión. Quiere intentar nuevamente?"
@try_again = gets.chomp.capitalize
end
end
end
loop do
filename= gets.chomp
if File.exist?("#{filename}")
description = gets.chomp
state = gets.chomp.capitalize
if state == "Si"
state = true;
elsif state == "No"
state = false;
end
open(filename, "r") { |file| @contenido = file.read() }
content = @contenido
uri = URI.parse("https://api.github.com/gists")
request = Net::HTTP::Post.new(uri)
gist = Gist.new(filename,description,state,content)
gist.post(uri, request)
break if gist.response_status == "201"
break if gist.try_again == "No"
else
puts "The file does not exist"
continue = gets.chomp.capitalize
break if continue == "No"
end
end
我想用rspec测试测试用例,但是没看懂
我想到要创建一个 Gist 作为测试用例。例如,我考虑过检查 returns Gist 状态的变量是否等于“201”,但这对我不起作用。
这是我的 rspec 文件:
require './git_clases.rb'
RSpec.describe Gist do
describe "#Gist" do
it "#Gist created" do
expect(response_status)==("201")
end
end
end
response_status
是 Gist
对象上的一个方法。您需要创建一个 Gist
对象,对其调用 post
,并在您的对象上检查 response_status
。
RSpec.describe Gist do
# Generally organize by method
describe "#post" do
# Example descriptions read grammatically correct
# "Gist #post creates a gist"
it "creates a gist" do
filename = "test.txt"
description = "testing gist"
state = "dunno what goes here"
content = "some test content"
gist = described_class.new(filename, description, state, content)
uri = URI.parse("https://api.github.com/gists")
request = Net::HTTP::Post.new(uri)
gist.post(uri, request)
expect(gist.response_status).to eq 201
end
end
end
described_class
是 RSpec.describe Gist
的 Gist
。最好将 class 名称硬编码到示例中,以防示例被共享或 class 名称更改。
expect(gist.response_status).to eq 201
真的是expect(gist.response_status).to( eq(201) )
。这使用匹配器 eq(201)
来比较 gist.response_status
,匹配器 eq(201)
只是检查它是否等于 201。这对于简单的相等性检查来说似乎有点过分,但它允许 rspec
提供 a wide variety of complex matchers以及自定义的。
您的 post
方法有点奇怪,因为用户需要初始化并传入 URL 和 Request 对象。这可能应该由 Gist 完成。
不需要将response_status转成字符串。最好将其保留为整数。如果确实需要它作为字符串,请使用 response.code.to_s
.
我的代码使用 github API 创建了一个 github 要点。创建要点后, API returns 给我一个状态码。如果代码是“201”,则创建了要点。但如果它是“400”,就会出错。在我的代码中,保存该状态的变量是 response_status
这是我的代码:
require 'net/http'
require 'json'
require 'uri'
class Gist
attr_reader :response_status
attr_reader :try_again
def initialize(filename,description,state,content)
@filename = filename
@description = description
@state = state
@content = content
end
def post(uri, request)
request.basic_auth("my_username", "my_token")
request.body = JSON.dump({
"description" => @description,
"public" => @state,
"files" => {
@filename => {
"content" => @content
}
}
})
req_options = { use_ssl: uri.scheme == "https" }
begin
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
json = response.body
parsed = JSON.parse(json)
@response_status = "#{ response.code }"
if @response_status == "201"
puts "Tu gist se ha creado con exito. La URL de su gist es: "+ parsed["url"]
end
rescue SocketError => se
puts "Ha ocurrido un error de conexión. Quiere intentar nuevamente?"
@try_again = gets.chomp.capitalize
end
end
end
loop do
filename= gets.chomp
if File.exist?("#{filename}")
description = gets.chomp
state = gets.chomp.capitalize
if state == "Si"
state = true;
elsif state == "No"
state = false;
end
open(filename, "r") { |file| @contenido = file.read() }
content = @contenido
uri = URI.parse("https://api.github.com/gists")
request = Net::HTTP::Post.new(uri)
gist = Gist.new(filename,description,state,content)
gist.post(uri, request)
break if gist.response_status == "201"
break if gist.try_again == "No"
else
puts "The file does not exist"
continue = gets.chomp.capitalize
break if continue == "No"
end
end
我想用rspec测试测试用例,但是没看懂
我想到要创建一个 Gist 作为测试用例。例如,我考虑过检查 returns Gist 状态的变量是否等于“201”,但这对我不起作用。
这是我的 rspec 文件:
require './git_clases.rb'
RSpec.describe Gist do
describe "#Gist" do
it "#Gist created" do
expect(response_status)==("201")
end
end
end
response_status
是 Gist
对象上的一个方法。您需要创建一个 Gist
对象,对其调用 post
,并在您的对象上检查 response_status
。
RSpec.describe Gist do
# Generally organize by method
describe "#post" do
# Example descriptions read grammatically correct
# "Gist #post creates a gist"
it "creates a gist" do
filename = "test.txt"
description = "testing gist"
state = "dunno what goes here"
content = "some test content"
gist = described_class.new(filename, description, state, content)
uri = URI.parse("https://api.github.com/gists")
request = Net::HTTP::Post.new(uri)
gist.post(uri, request)
expect(gist.response_status).to eq 201
end
end
end
described_class
是 RSpec.describe Gist
的 Gist
。最好将 class 名称硬编码到示例中,以防示例被共享或 class 名称更改。
expect(gist.response_status).to eq 201
真的是expect(gist.response_status).to( eq(201) )
。这使用匹配器 eq(201)
来比较 gist.response_status
,匹配器 eq(201)
只是检查它是否等于 201。这对于简单的相等性检查来说似乎有点过分,但它允许 rspec
提供 a wide variety of complex matchers以及自定义的。
您的 post
方法有点奇怪,因为用户需要初始化并传入 URL 和 Request 对象。这可能应该由 Gist 完成。
不需要将response_status转成字符串。最好将其保留为整数。如果确实需要它作为字符串,请使用 response.code.to_s
.