Skip to content

add 'test' support, allows content of responses to be checked #63

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

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ afterAll (done) ->

If `beforeAll`, `afterAll`, `before` and `after` are called multiple times, the callbacks are executed serially in the order they were called.

**Abao** provides hook to allow the content of the response to be
checked within the test:

```coffee
{test} = require 'hooks'

test 'GET /machines -> 200', (response, body, done) ->
assert.deepEqual(JSON.parse(body), ["machine1", "machine2"])
assert.equal(headers["content-type"], 'application/json; charset=utf-8')
return done()
```

### test.request

- `server` - Server address, provided from CLI.
Expand Down
12 changes: 6 additions & 6 deletions lib/abao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Abao
factory = new TestFactory(config.options.schemas)

async.waterfall [
# Parse hooks
(callback) ->
addHooks hooks, config.options.hookfiles
callback()
,
# Load RAML
(callback) ->
raml.loadFile(config.ramlPath).then (raml) ->
Expand All @@ -35,12 +40,7 @@ class Abao
,
# Parse tests from RAML
(raml, callback) ->
addTests raml, tests, callback, factory
,
# Parse hooks
(callback) ->
addHooks hooks, config.options.hookfiles
callback()
addTests raml, tests, hooks, callback, factory
,
# Run tests
(callback) ->
Expand Down
1 change: 0 additions & 1 deletion lib/add-hooks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ path = require 'path'
require 'coffee-script/register'
proxyquire = require('proxyquire').noCallThru()
glob = require 'glob'
async = require 'async'


addHooks = (hooks, pattern) ->
Expand Down
13 changes: 6 additions & 7 deletions lib/add-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ parseHeaders = (raml) ->
headers

# addTests(raml, tests, [parent], callback, config)
addTests = (raml, tests, parent, callback, testFactory) ->
addTests = (raml, tests, hooks, parent, callback, testFactory) ->

# Handle 3th optional param
# Handle 4th optional param
if _.isFunction(parent)
testFactory = callback
callback = parent
Expand Down Expand Up @@ -56,13 +56,12 @@ addTests = (raml, tests, parent, callback, testFactory) ->
# Iterate response status
for status, res of api.responses

testName = "#{method} #{path} -> #{status}"

# Append new test to tests
test = testFactory.create()
test = testFactory.create(testName, hooks.contentTests[testName])
tests.push test

# Update test
test.name = "#{method} #{path} -> #{status}"

# Update test.request
test.request.path = path
test.request.method = method
Expand All @@ -86,7 +85,7 @@ addTests = (raml, tests, parent, callback, testFactory) ->
return callback(err) if err

# Recursive
addTests resource, tests, {path, params}, callback, testFactory
addTests resource, tests, hooks, {path, params}, callback, testFactory
, callback


Expand Down
6 changes: 6 additions & 0 deletions lib/hooks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Hooks
@afterAllHooks = []
@beforeEachHooks = []
@afterEachHooks = []
@contentTests = {}

before: (name, hook) =>
@addHook(@beforeHooks, name, hook)
Expand All @@ -34,6 +35,11 @@ class Hooks
else
hooks[name] = [hook]

test: (name, hook) =>
if @contentTests[name]?
throw new Error("Cannot have more than one test with the name: #{name}")
@contentTests[name] = hook

runBeforeAll: (callback) =>
async.series @beforeAllHooks, (err, results) ->
callback(err)
Expand Down
15 changes: 10 additions & 5 deletions lib/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class TestFactory
for file in files
tv4.addSchema(JSON.parse(fs.readFileSync(file, 'utf8')))

create: () ->
return new Test()
create: (name, contentTest) ->
return new Test(name, contentTest)

class Test
constructor: () ->
@name = ''
constructor: (@name, @contentTest) ->
@name ?= ''
@skip = false

@request =
Expand All @@ -47,6 +47,9 @@ class Test
headers: null
body: null

@contentTest ?= (response, body, done) ->
done()

url: () ->
path = @request.server + @request.path

Expand All @@ -56,6 +59,7 @@ class Test

run: (callback) ->
assertResponse = @assertResponse
contentTest = @contentTest

options = _.pick @request, 'headers', 'method'
options['url'] = @url()
Expand All @@ -69,7 +73,7 @@ class Test
,
(error, response, body, callback) ->
assertResponse(error, response, body)
callback()
contentTest(response, body, callback)
], callback

assertResponse: (error, response, body) =>
Expand All @@ -82,6 +86,7 @@ class Test
#{body}
Error
"""
response.status = response.statusCode

# Body
if @response.schema
Expand Down
16 changes: 8 additions & 8 deletions test/unit/add-tests-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ proxyquire = require('proxyquire').noCallThru()
mochaStub = require 'mocha'

TestFactory = require '../../lib/test'
hooks = require '../../lib/hooks'
addTests = proxyquire '../../lib/add-tests', {
'mocha': mochaStub
}
Expand All @@ -22,13 +23,12 @@ describe '#addTests', ->
callback = ''

before (done) ->

ramlParser.loadFile("#{__dirname}/../fixtures/single-get.raml")
.then (data) ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done
after ->
tests = []
Expand Down Expand Up @@ -76,7 +76,7 @@ describe '#addTests', ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done
after ->
tests = []
Expand Down Expand Up @@ -123,7 +123,7 @@ describe '#addTests', ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done
after ->
tests = []
Expand Down Expand Up @@ -167,7 +167,7 @@ describe '#addTests', ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done
after ->
tests = []
Expand Down Expand Up @@ -211,7 +211,7 @@ describe '#addTests', ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done

after ->
Expand Down Expand Up @@ -251,7 +251,7 @@ describe '#addTests', ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done

after ->
Expand Down Expand Up @@ -295,7 +295,7 @@ describe '#addTests', ->
callback.returns(done())

sinon.stub console, 'warn'
addTests data, tests, callback, testFactory
addTests data, tests, hooks, callback, testFactory
, done

after ->
Expand Down
24 changes: 24 additions & 0 deletions test/unit/hooks-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,27 @@ describe 'Hooks', () ->
it 'should run hook', ->
assert.ok funcs[2].called
assert.ok funcs[3].called

describe 'when successfully adding test hook', () ->

afterEach () ->
hooks.contentTests = {}

test_name = "content_test_test"

it 'should get added to the set of hooks', () ->
hooks.test(test_name, () ->)
assert.isDefined(hooks.contentTests[test_name])

describe 'adding two content tests fails', () ->
afterEach () ->
hooks.contentTests = {}

test_name = "content_test_test"

it 'should assert when adding a second content test', () ->
f = () ->
hooks.test(test_name, () ->)
f()
assert.throw f,
"Cannot have more than one test with the name: #{test_name}"
11 changes: 11 additions & 0 deletions test/unit/test-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ describe 'Test', ->
testFact = ''
test = ''
machine = ''
contentTestCalled = null

before (done) ->

testFact = new TestFactory()
test = testFact.create()
contentTestCalled = false
test.name = 'POST /machines -> 201'
test.request.server = 'http://abao.io'
test.request.path = '/machines'
Expand All @@ -50,6 +52,12 @@ describe 'Test', ->
type: 'foo'
name: 'bar'

test.contentTest = (response, body, done) ->
contentTestCalled = true
assert.equal(response.status, 201)
assert.deepEqual(JSON.parse(body), machine)
return done()

requestStub.callsArgWith(1, null, {statusCode: 201}, JSON.stringify(machine))
test.run done

Expand Down Expand Up @@ -88,6 +96,9 @@ describe 'Test', ->
# assert.equal response.headers, 201
assert.deepEqual response.body, machine

it 'should call contentTest', ->
assert.isTrue contentTestCalled


describe 'of test contains params', ->

Expand Down