我以前从未使用过 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
应用规范.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
第一次编辑:
我的应用.rb
require 'dotenv'
Dotenv.load
require 'yaml'
require 'sinatra'
require 'helpers'
require 'actions'
require '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
最佳答案
您的规范助手实际上应该是:
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
让它正常工作,然后您可以在添加更多测试时进行重构。
关于ruby - Rspec 和 sinatra 对我不起作用,但我希望它们能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177287/