-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add helper methods to ease controller testing #10
Comments
@epergo thanks for opening this issue. So Garnet already has something like this. It might look a little cumbersome but you can simply do this to test your Controller class BlogControllerTest < GarnetSpec::Controller::Test
getter handler : Amber::Pipe::Pipeline
def initialize
@handler = Amber::Server.instance.handler
@handler.prepare_pipelines
end
end Then write your unit tests as describe BlogControllerTest do
subject = BlogControllerTest.new
it "renders blog index template" do
Blog.clear
response = subject.get "/blogs"
response.status_code.should eq(200)
response.body.should contain("Blogs")
end
end Let me know if this is helpful |
Issue: #10 Garnet should supports all the HTTP verbs available for simulating requests in a conntroller test. Currently is missing the Options, Trace, and Connect http verbs Adds missing HTTP Read Verbs to generate helper methods for testing controllers. With these changes Garnet has available all the HTTP verbs needed.
@eliasjpr it seems like the |
Hi, For my project what I have done is the following: macro define_request_methods(handler)
{{handler}}.prepare_pipelines
{% http_read_verbs = %w(get head) %}
{% http_write_verbs = %w(post put patch delete) %}
{% http_verbs = http_read_verbs + http_write_verbs %}
{% for method in http_verbs %}
def {{method.id}}(path, headers : HTTP::Headers? = nil, body : String? = nil)
request = HTTP::Request.new("{{method.id}}".upcase, path, headers, body)
{% if http_write_verbs.includes?(method) %}
request.headers["Content-Type"] = "application/json"
{% end %}
process_request({{ handler }}, request)
end
{% end %}
def process_request(router, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
router.call(context)
response.close
io.rewind
HTTP::Client::Response.from_io(io, decompress: false)
end
end
define_request_methods(Amber::Server.handler) I have mixed Garnet code generation for http verbs methods and the routes of my application. So now I can do this: require "../../../spec_helper"
describe(V1::AwesomeController) do
describe("index") do
after { clean }
describe("when there are resources") do
it "should return all" do
(0..2).each { |i| create_resources(i) }
response = get("/api/v1/resources")
parsed_body = JSON.parse(response.body)
expect(parsed_body["data"].size).must_equal(Resource.all.size)
expect(response.status_code).must_equal(200)
end
end
end
end So I don't have to create a Mock Controller to test |
@robacarp is a little tricky since we do not have any Amber reference in Garnet. So we would have to somehow define the |
@epergo the {{handler}}.prepare_pipelines if {{handler}}.responds_to? :prepare_pipelines Also I personally prefer the inheritance approach since it keeps the request methods withing the scope of the test and not mixed other methods. I think this would work. WDYT? |
Good points @eliasjpr , I didn't think about coupling with Amber because we were in a repository created under the scope of Amber, but you are right! About inheritance, could it be where DISCLAIMER I don't know if this is possible, just an idea/dream require "../../../spec_helper"
describe(V1::AwesomeController) do
include RequestHelpers
describe("index") do
after { clean }
describe("when there are resources") do
it "should return all" do
(0..2).each { |i| create_resources(i) }
response = get("/api/v1/resources")
parsed_body = JSON.parse(response.body)
expect(parsed_body["data"].size).must_equal(Resource.all.size)
expect(response.status_code).must_equal(200)
end
end
end
end |
Issue: #10 Garnet should supports all the HTTP verbs available for simulating requests in a conntroller test. Currently is missing the Options, Trace, and Connect http verbs Adds missing HTTP Read Verbs to generate helper methods for testing controllers. With these changes Garnet has available all the HTTP verbs needed.
I'm having trouble getting started with Amber, my tests aren't compiling and can't seem to find this class
How are we supposed to write controller tests? |
Right know is possible to test controller actions preloading routes of your application and borrowing a helper method from Amber's source.
For example, for making an HTTP GET request we could do the following:
With that a user could test its route as this:
It would be very useful to have a method for each HTTP verb to make requests and be able to attach params and headers as well.
The text was updated successfully, but these errors were encountered: