【问题标题】:Minitest with Sinatra (Ruby)Minitest 与 Sinatra (Ruby)
【发布时间】:2018-08-26 13:56:43
【问题描述】:

我正在尝试将 minitest 与 sintara 一起使用,我的问题是运行测试 (ruby test_login.rb) 无法找到登录页面,当我打印出文档 html 时,我得到了 sinatra 404 页面。我不知道如何将我的网络应用程序与这个测试程序连接起来,我搜索过的所有文档和以前的问题都没有任何帮助。

这是我的代码:

require 'sinatra/base'
require 'minitest/autorun'
require 'rack/test'
require 'minitest/spec'
require 'nokogiri'
require 'rack/test'
require_relative 'login'

class Test < MiniTest::Test`

  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  def test_login
    response = get ('/home')
    doc = Nokogiri::HTML(response.body)
    puts last_response
    puts doc
    #response = post '/login', username: 'test_user', password: 'password'
    #get '/home'
    #follow_redirect!()
    #puts doc
    #assert_equal "Admin", doc.at_css("#admin-block div h1")
  end
end

请不要评论要求我使用不同的测试 gem。

谢谢

【问题讨论】:

  • 您应该至少展示您的 Sinatra 应用程序的一些相关部分。由于(我假设)response = get ('/home') 有效,我想这不是 Minitest 或 Rack::Test 的问题,而是应用程序本身的问题。
  • response = get ('/home') 无法正常工作,因为控制台的输出是 404 页面,应该是主页。

标签: ruby testing sinatra rack minitest


【解决方案1】:

由于您正在测试登录是否有效,您只想查看您传递给登录表单的登录信息是否确实给出了HTTP 200 响应。

您的示例中无需注释代码的第一部分。 作为旁注,检查来自页面的响应是否具有包含页面正文内容的特定 HTTP 代码不被视为“好”测试,但如果您认为它足够体面,那么继续它。

如果您传递的登录信息有效,则以下测试应该可以工作。

def test_login
  response = post '/login', username: 'test_user', password: 'password'
  assert_equal response.status, Net::HTTPSuccess
end

编辑

在发布我的答案后立即更新了问题,但如果您在向 /home 发送 GET 请求时遇到错误,听起来好像您的应用程序中没有定义该路由。

【讨论】:

  • 1) 错误:Test#test_login: NameError: uninitialized constant Test::Net test_login.rb:31:in test_login' Is the error I receive. assert_equal response.status, Net::HTTPSuccess` 是有问题的代码.
  • 您必须require 'net/http' 才能使用。
  • 回到原来的问题:404 #运行:

    Sinatra 不知道这个小曲。 example.org/__sinatra__/404.png">
    试试这个:
    post '/login' do "Hello World" end 

  • 这个link可能与这个问题有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多