0

I have trouble running my rspec tests. I am making an application that creates a Gist, using the Github API, I have a test case in rspec that verifies if Gist was created by simulating a request to the API. However, my rspec tests do not simulate, but instead run my program normally. For example, if I run $ ruby ​​git_clases.rb I get the same result.

My structure looks like this:

├── Gemfile
├── Gemfile.lock
├── git_clases.rb
└── spec
    ├── git_clases_spec.rb
    └── spec_helper.rb

git_clases.rb:

require 'net/http'
require 'json'
require 'uri'
require 'dotenv/load'
require 'rspec'

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(ENV['YOUR_USERNAME'], ENV['YOUR_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 "Your gist has been created successfully. The URL of your gist is: "+ parsed["url"]
        end

        rescue SocketError => se
            puts "A connection error has occurred. Want to try again?"
            @try_again = gets.chomp.capitalize
        end
    end
end

loop do
    puts "Filename?"
    filename = gets.chomp

    if File.exist?(filename) 
        puts "Description?"
        description = gets.chomp
        puts "Public or Private?"
        state = gets.chomp.capitalize

        if state == "Yes" 
            state = true;
        elsif state == "No"
            state = false;
        else
            puts "Yes or no..."
        end

        open(filename, "r") { |file| @content_file = file.read() } 
        content = @content_file
        GITHUB_URL = "https://api.github.com/gists"
        uri = URI.parse(GITHUB_URL)
        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 "Your file does not exist"
        puts "Do you want try again?"
        continue = gets.chomp.capitalize
        break if continue == "No"
    end
end

spec/git_clases_spec:

require_relative '../git_clases.rb'

RSpec.describe Gist do
  describe "#post" do
    it "crear gist" do
      filename = "test.txt"
      description = "descripción"
      state = true
      content = "contenido"

      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 output:

Filename?

I think that rspec should not run my program when I run the "rspec" command

Any ideas? Thanks you

  • 1
    RSpec needs to load the file and interpret it to run specs against it. But in that file, you wrote code that is not just defining a class or a method but immediately runs something (the whole `loop` block) that block is – of course – run be RSpec when it loads the file. – spickermann Feb 26 '20 at 12:30
  • So, should I delete the loop? – Joel Alayon Feb 26 '20 at 12:39
  • 1
    I would move it into a method or even better a class or module ion its own. Something like `GistManager` might be a good name for that... – spickermann Feb 26 '20 at 12:43
  • Additionally, those specs aren't consistent with how specs should be written; all of those variables should be moved out of the example (it) block and defined as let vars. I recommend reading through http://www.betterspecs.org/ These might also help: https://stackoverflow.com/questions/59486571/stub-faraday-request-with-webmock-help-needed/59505757#59505757 https://stackoverflow.com/questions/58085529/how-to-properly-mock-itnernal-services-in-rspec/58125881#58125881 https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher – Allison Feb 28 '20 at 06:34

0 Answers0