Ruby 如何正确使用rspec?

Ruby 如何正确使用rspec?,ruby,testing,rspec,Ruby,Testing,Rspec,我的代码使用github API创建github gist。创建gist后,API将向我返回一个状态代码。如果代码为“201”,则创建了要点。但如果是“400”,则有一个错误。在我的代码中,保存该状态的变量是response\u status 这是我的代码: require 'net/http' require 'json' require 'uri' class Gist attr_reader :response_status attr_reader :try_again

我的代码使用github API创建github gist。创建gist后,API将向我返回一个状态代码。如果代码为“201”,则创建了要点。但如果是“400”,则有一个错误。在我的代码中,保存该状态的变量是response\u 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状态的变量是否等于“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\u status
是对
Gist
对象的一种方法。您需要创建一个
Gist
对象,在其上调用
post
,并检查对象上的
response\u 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
RSpec中的
Gist
。描述Gist
。最好将类名硬编码到示例中,以防示例共享或类名更改

expect(gist.response\u status)。to eq 201
实际上是
expect(gist.response\u status)。to(eq(201))
。这将使用匹配器
eq(201)
比较gist.response\u status,该匹配器只检查它是否等于201。对于一个简单的平等性检查来说,这似乎有点过分,但它允许
rspec
提供自定义检查


您的
post
方法有点奇怪,因为用户需要初始化并传入URL和请求对象。这可能应该由Gist来完成

无需将响应_状态转换为字符串。最好将其保留为整数。如果您确实需要它作为字符串,请使用