leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.12k stars 247 forks source link

"attempt to call field 'get_phase' (a nil value)" when trying to mock request #743

Closed eko234 closed 1 year ago

eko234 commented 3 years ago

I'm trying to run some tests with busted.

fennel = require("lib.fennel")
package.path = package.path .. ';../?.fnl'
table.insert(package.loaders or package.searchers, fennel.searcher)
pp = function(x) print(require("lib.fennelview")(x)) end
local lapis = require("lapis.application")
local mock_request = require("lapis.spec.request").mock_request
local mount_auth = require("controls.auth").mount_auth
local cjson = require("cjson")
local use_test_env = require("lapis.spec").use_test_env

local app = lapis.Application()
mount_auth(app)

describe("the api", function()
  use_test_env()
  it("provides a authentication module", function()
    local status, body
    = mock_request(app, "/auth/login",
    { method = "POST"
    , body= cjson.encode{email="wayne@moon.com",password="asdasd"}
    , headers = {["content-type"] = "application/json"}})
    print(body)
    assert.same(200, status)
  end)
end)

that endpoint returns a table containing the user information if the login is successful, when I alter the endpoint handler to just return the json that was sent, it works well, but when I perform actions against a database I get this error:

"/usr/local/share/lua/lua5.1/pgmoon/socket.lua:84: attempt to call field 'get_phase' (a nil value)"

fearless-spider commented 2 years ago

Hi @eko234 , I have the same issue, something is wrong with connection to DB or test config.

leafo commented 1 year ago

Closing out old issues: I've since changed how db connections are established to detect if nginx cosocks are able to be used. If the code running is in part of the request that can't use cosockets then it will fall back to using luasocket (note that these requests will be blocking)