【问题标题】:Rspec and sinatra just not working for me but I want them toRspec 和 sinatra 只是不适合我,但我希望他们能
【发布时间】:2014-10-03 19:10:21
【问题描述】:

我以前从未使用过 Sinatra,也从未手动配置过 Rspec(一直使用预先编写的 rails 脚本),但我想试一试。

但是我遇到了麻烦,我设法让 RSpec 工作,但我遇到了错误,只是让它识别来自 Sinatra 的方法。

我想知道改用Rack::Test 是否会更好。

我目前的问题是atm:

1) rake 失败并显示 Don't know how to build task 'default'

2) 当我使用rspec 时,它会因undefined method get for #<RSpec::ExampleGroups::MySinatraApplication:0 而失败

现在显然我做错了什么,但我不知道是什么。我正在关注我发现的一些 tuts,但是很好,我只是不太顺利。

耙文件:

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

第一次编辑:

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

【问题讨论】:

    标签: ruby rspec sinatra


    【解决方案1】:

    您的规范助手应该是:

    require 'rspec'
    require 'rack/test'
    
    RSpec.configure do |conf|
      conf.include Rack::Test::Methods
    end
    

    然后在您的个人测试中:

    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
    

    让它工作,然后您可以在添加更多测试时进行重构。

    【讨论】:

    • 我不明白,但它可以在浏览器中运行,但测试失败并显示“未找到”响应。
    • 欢迎测试,是时候弄清楚它在做什么了。我回答了你的问题,所以请接受我的回答。
    • 部分答案会打勾吗?
    • 根据我提供的设置,您现在有不同的问题,对吧?欢迎您在另一个问题中发布另一个失败。
    • 我的编辑出了什么问题?无论如何,我问的问题是两个部分,第一个是为什么我的 rake 不起作用,我通过删除一行代码来修复它,现在 rake 有效。另外,刚刚找到另一个问题的解决方案,Sinatra::Application 应该是@app = AppName。 Sill,您确实提供了帮助,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2022-01-14
    相关资源
    最近更新 更多