无法在Test-Unit Sinatra中加载此类文件

时间:2016-07-05 05:34:23

标签: ruby unit-testing sinatra

我目前正在使用ruby sinatra编写我的测试用例,我在使用Test-Unit sinatra编写测试用例时非常奇怪或只是错过了一个重要的注释/提示。

我的问题是我的环境。我的app.rb没有在我的测试中加载.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

我的environment.rb位于配置文件夹中。

这是我的示例测试用例。 (尚未完成)

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>

我很困惑我做错了什么。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了答案,只需将'./config/environments'更改为require_relative。并将require'../ app / app'更改为require_into。