2016-07-05 82 views
0

我正在使用ruby sinatra編寫我的測試用例,並且使用測試單元sinatra編寫測試用例時,我有一個非常奇怪或者僅僅缺少重要註釋/提示。無法在測試單元中加載這樣的文件Sinatra

我的問題是我的我的environments.rb沒有從我的app.rb加載到我的測試中。但如果我運行它,它會得到加載。

這裏是我的app.rb

require 'rubygems' 
require 'sinatra' 
require 'pg' 
require './config/environments' 
require './models/user' 

module Registration 
    class HelloWorldApp < Sinatra::Base 

    helpers do 
     include Rack::Utils 
     alias_method :h, :escape_html 
    end 
    get '/' do 
     @title = " Introduction Biatch" 
     erb :index 
    end 

    post '/register' do 
     DB[:users].insert(username: params[:username],password: params[:password]) 
     redirect '/view' 
    end 

    get '/view' do 
     @users = User.all 
     erb :view 
    end 

    get '/view/:id' do 
     @user = User.find(id: params[:id]) 
     erb :edit 
    end 

    post '/edit/:id' do 
     @user = User.find(id: params[:id]) 
     @user.update(username: params[:username],password: params[:password]) 
     redirect '/view' 
    end 

    get '/delete/:id' do 
     @delete_user = User.find(id: params[:id]) 
     @delete_user.delete 
     redirect '/view' 
    end 

    end 
end 

我environments.rb是在config文件夾。

這裏是我的示例測試用例。 (尚未完成)

require 'rubygems' 
require 'test/unit' 
require 'test/unit/assertions' 
require '../app/app' 

module Registration 

    class TestCrud < Test::Unit::TestCase 
     # include Registration::HelloWorldApp 

     def test_insert_user 
     end 

     def test_get_all_user 
     end 

     def test_delete_all_user 
     end 

    end 

end 

並且這是它引發的錯誤。

C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./config/environments (LoadError) 
     from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
     from C:/Users/John/Documents/Sinatra-Intro/app/app.rb:4:in `<top (required)>' 
     from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
     from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
     from test_crud.rb:4:in `<main>' 

C:\Users\John\Documents\Sinatra-Intro\test> 

我很困惑我做錯了什麼。提前致謝。

回答

0

我找到了答案,只需將require'./config/environments'更改爲require_relative。並將require'../app/app'更改爲require_into。

相關問題