【发布时间】:2016-11-02 15:19:45
【问题描述】:
我正在尝试在我的代码中测试该方法,但第二个测试返回错误undefined local variable or method 'params'
测试方法的正确方法是什么?或者我需要对main.rb 的设置方式进行更改吗?
代码:
require 'sinatra'
require 'sinatra/reloader'
def get_products_of_all_ints_except_at_index()
@array = [1, 7, 3, 4]
@total = 1
@index = params[:index].to_i
@array.delete_at(@index)
@array.each do |i|
@total *= i
end
end
get '/' do
get_products_of_all_ints_except_at_index
erb :home
end
测试:
ENV['RACK_ENV'] = 'test'
require 'minitest/autorun'
require 'rack/test'
require_relative 'main.rb'
include Rack::Test::Methods
def app
Sinatra::Application
end
describe 'app' do
it 'should return something' do
get '/'
assert_equal(200, last_response.status)
end
it 'should return correct result' do
get_products_of_all_ints_except_at_index
assert_equal(24, @total)
end
end
【问题讨论】: