Created
March 25, 2012 18:57
-
-
Save toolmantim/2199021 to your computer and use it in GitHub Desktop.
Simpler test helpers for Goliath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Additional methods for Goliath::TestHelper to make simple Goliath testing | |
| # simple. You might need the other syntax for testing gnarly stuff, but I've | |
| # yet to need it. To use, just include this in your spec_helper.rb. | |
| # | |
| # Your spec must respond to #api and return the Goliath API class to test. | |
| # | |
| # @example | |
| # describe MyAPI do | |
| # let(:api) { MyAPI } | |
| # describe "GET /" do | |
| # subject { get("/") } | |
| # its("response_header.status") { should == 200 } | |
| # end | |
| # end | |
| # | |
| # It's just the same if you're using #before instead of #subject: | |
| # | |
| # @example | |
| # describe MyAPI do | |
| # let(:api) { MyAPI } | |
| # describe "GET /" do | |
| # before do | |
| # @client = get("/") | |
| # end | |
| # it "has a 200 response" do | |
| # @client.response_header.status.should == 200 | |
| # end | |
| # end | |
| # end | |
| # | |
| # If you need to execute within the EM reactor use the block syntax: | |
| # | |
| # @example | |
| # describe MyAPI do | |
| # let(:api) { MyAPI } | |
| # describe "GET /" do | |
| # get("/") do |client| | |
| # @client.response_header.status.should == 200 | |
| # end | |
| # end | |
| # end | |
| module Goliath::TestHelper | |
| def get path, opts={}, &callback | |
| server_request :get, path, opts, &callback | |
| end | |
| def post path, opts={}, &callback | |
| server_request :post, path, opts, &callback | |
| end | |
| def put path, opts={}, &callback | |
| server_request :put, path, opts, &callback | |
| end | |
| def delete path, opts={}, &callback | |
| server_request :delete, path, opts, &callback | |
| end | |
| def server_request method, path, opts={}, &callback | |
| req = nil | |
| with_api(api) do | |
| req = EM::HttpRequest.new("http://localhost:#{@test_server_port}#{path}").__send__(method, opts) | |
| req.callback &callback | |
| req.callback { stop } | |
| req.errback { stop } | |
| req | |
| end | |
| req | |
| end | |
| end |
Author
Ah, damn about the notifications. I had thought maybe that had been the case.
Cool, I can do a pull request, but first let me have a stab at the block-less syntax, passing another block that saves the return value somewhere, and then return it after the EM.stop happens.
Author
Just pushed a new version above that supports the block and block-less syntax…
Ah, nice. Yea if you don't need to use the reactor that should work well.
Hey guys, sorry, running way behind here.. @toolmantim that looks pretty nice. Could you convert this into a pull on the repo, and we'll continue there?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a side note, I didn't seem to get any notification about your @dj2, so I'm not sure if that works in gist's. Just happened to notice this on twitter.