gpt4 book ai didi

ruby - Minitest:如何测试方法

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:56 26 4
gpt4 key购买 nike

我正在尝试在我的代码中测试该方法,但第二个测试返回错误 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

最佳答案

您没有通过 get 请求传递任何参数,请尝试:

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

关于ruby - Minitest:如何测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38124623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com