lapis icon indicating copy to clipboard operation
lapis copied to clipboard

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

Open eko234 opened this issue 2 years ago • 1 comments

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="[email protected]",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)"

eko234 avatar Aug 12 '21 00:08 eko234

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

fearless-spider avatar Sep 08 '22 19:09 fearless-spider

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)

leafo avatar Nov 07 '22 21:11 leafo