Thermatix
Thermatix

Reputation: 2929

Rspec and sinatra just not working for me but I want them to

I've never used Sinatra before and I've never manually configured Rspec before (always used pre-written script for rails) but I wanted to give it a go.

But I'm having trouble, I managed to get RSpec to sort of work but I'm running into errors just getting it to recognise methods from Sinatra.

I'm wondering if it would be better to switch to Rack::Test instead.

My current problems are atm:

1) rake fails with Don't know how to build task 'default'

2) when I use rspec it fails with undefined method get for #<RSpec::ExampleGroups::MySinatraApplication:0

Now obviously I'm doing something wrong, but I don't know what. I'm following some tuts I found but well ,I'ts just not going well.

RakeFile:

require 'rspec/core/rake_task'


RSpec::Core::RakeTask.new do |task|
  task.rspec_opts = ['-c', '-f progress', '-r ./spec/spec_helper.rb']
  task.pattern = './spec/**/*_spec.rb'
end

spec_helper.rb

require 'rspec'
require 'rack/test'

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

app_spec.rb

ENV['RACK_ENV'] = 'test'

require '../../myapp'
require 'rspec'
require 'rack/test'

describe 'My Sinatra Application' do

  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  it "says hello" do
    get '/' do
      expect(last_response).to be_ok
      expect(last_response.body).to eq('Hello World')
    end
  end


  it 'should allow access to main page' do

  end

  it 'should list every site from the links file' do
    # get '/' do
    #   Links.each do |link|
    #
    #   end
    # end
  end

end

First Edit:

myapp.rb

require 'dotenv'
Dotenv.load
require 'yaml'
require 'sinatra'
require 'helpers'
require 'actions'
require 'main'

main.rb

class Ops_JustGiving < Sinatra::Base
  Links = YAML::Load(File.open('..\\links.yml'))['sites']
  set :root, File.dirname __FILE__

  helpers Sinatra::Ops_JustGiving::Helpers

  register Sinatra::Ops_JustGiving::Actions

end

Upvotes: 1

Views: 1675

Answers (1)

Anthony
Anthony

Reputation: 15957

Your spec helper should really just be:

require 'rspec'
require 'rack/test'

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

Then in your individual tests:

ENV['RACK_ENV'] = 'test'

require 'hello_world'  # <-- your sinatra app name
require 'rspec'
require 'rack/test'

describe 'My Sinatra Application' do
  include Rack::Test::Methods  #<---- you really need this mixin

  def app
    Sinatra::Application
  end

  it "says hello" do
    get '/'
    expect(last_response).to be_ok
    expect(last_response.body).to eq('Hello World')
  end
end

Get that working then you can refactor as your add more tests.

Upvotes: 6

Related Questions