-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testing controllers #57
Comments
Right now, things are a bit weird as there was a recent, breaking change with how controllers operate. The old way (pre 0.4.0-dev) was more custom. The new way relies on the Plug specification. v0.4.0 should be published to Hex in the next few days as I'm currently working on a few bugs that still remain from the recent changes. Considering that, I'll cover testing with the new controllers. (I'm sure the older controllers are testable, but I'm not sure the best of accomplishing that. Holding off just a little bit for the newest version will definitely be worth it.) Given this controller: defmodule MyController do
use Sugar.Controller
@doc false
def index(conn, []) do
# Somehow get our content
# Render our "mycontroller/index.html.eex" view
render conn
end
it can be tested as such: ## This is mostly from the Plug documentation over testing plugs.
## It's slightly modified for this case.
defmodule MyControllerTest do
use ExUnit.Case, async: true
use Plug.Test
test "returns hello world" do
# Create a test connection
conn = conn(:get, "/")
opts = MyController.init [ action: :index, args: %{} ]
# Invoke the controller
conn = MyController.call(conn, opts)
# Assert the response and status
assert conn.state == :sent
assert conn.status == 200
assert conn.resp_body == "Hello world"
end
end
The documentation for Sugar is pretty out of date, and I apologise for that. I've got plans to update it shortly with all of the newest changes, so let me know if anything is unclear. |
@slogsdon Thank you. I'm getting this error after update:
can't start mix server. Also how to set custom error pages (404 and 500) and how should I serve static assets? |
Templates don't auto update, you need to stop and start server to see changes. |
@NARKOZ The For capturing 500 errors, check out Static assets can be served with Thanks for the heads up about the templates not autoupdating. The |
@slogsdon Thank you. After config update I'm getting this error:
if I change the line to
My config: use Mix.Config
config :sugar,
router: Router
config :sugar, Router,
https_only: false I don't use SSL. |
@NARKOZ Apologies for the delay. The router was updated at some point to allow for HTTPS to be optional by setting the router config option for it to use MixConfig
config :sugar,
router: Router
config :sugar, Router,
https: false Closing this, but feel free to re-open. |
How can I write tests for controllers and test requests?
The text was updated successfully, but these errors were encountered: