2016-06-30 4 views
2

하는 방법을 테스트하기 위해,하지만 두 번째 테스트가 오류 undefined local variable or method 'params'Minitest : 어떻게 내 코드에서 방법을 테스트하기 위해 노력하고있어

방법을 테스트하는 올바른 방법은 무엇입니까를 반환? 또는 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 

답변

1

당신은 당신의 GET 요청 어떤 PARAMS 전달되지 않습니다보십시오 있기 때문에

get '/', :index => '1' 
+0

이 밖으로 도움이 될 것입니다 http://www.sinatrarb.com/testing.html 모든 모의 요청 메소드는 같은 인수 서명이 있습니다 GET '/ 경로'있는 params = {}, rack_env을 = {} – SickLickWill

0

발발했습니다 첫 번째 테스트 작동 get '/'에 전화 할 때 기본 params지도가 설정됩니다. 그러나 직접 호출 할 때 paramsnil이며 오류가 발생하는 이유입니다. 여기서 가장 좋은 방법은 필요한 데이터를 보내는 것입니다. 같은 뭔가 :

def get_products_of_all_ints_except_at_index index 
    @array = [1, 7, 3, 4] 
    @total = 1 
    @array.delete_at(index) 
    @array.each do |i| 
    @total *= i 
    end 
end 

get '/' do 
    get_products_of_all_ints_except_at_index params[:index].to_i 
    erb :home 
end 

요청에 물건을 찾기는 코드의 최 외층에서 할 일반적으로 좋다. 그렇다면 비즈니스 코드도 높은 테스트 가능성을 얻을 것입니다!