Skip to content

Commit

Permalink
added tests for lib/configurator.coffee
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbjensen committed Jul 2, 2011
1 parent 33c2d26 commit 4ce7caf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,13 @@ Right now we don't have a definitive answer, but we have a number of innovative

Yes, we know. At the moment there are very few tests. This is bad. We are currently evaluating testing frameworks in the hope of finding one we truly like. After all, we're likely to be stuck with it for a good many years, so we need to choose carefully. We will then begin writing unit and integration tests for parts of SocketStream which are unlikely to change in the near future. Right now, any help and contributions in this area would be very much appreciated.

--- Update ---

We're currently writing unit tests for the socketstream library, one file at a time, and using Jasmine. To run these tests:

npm install jasbin
cd socketstream
jasbin

### Known Issues

Expand Down
74 changes: 74 additions & 0 deletions spec/lib/configurator_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
describe "Configurator", ->

beforeEach ->
# Mock the SS global object
global.SS = {}
global.configurator = require '../../lib/configurator.coffee'
# Load the extensions.js helper functions
require '../../lib/extensions.js'
global.fs = require 'fs'
global.mode = 0755

# This is the public function for configurator.
# It loads the default app configuration,
# then it merges in the app's configuration,
# then it merges in the app's environment-specific
# configuration, if it exists.
#
describe "configure", ->
it "should set the SS.config's default values", ->
expect(SS.config).toEqual undefined
configurator.configure()
expect(typeof(SS.config)).toEqual "object"
# I might want to check that it loads certain values,
# but to do that, I would need to define the default
# config in an accessible way, and load that for
# comparison.

it "should set the SS.config's default environment values", ->
# Note, these values are statically defined in the test
# TODO - write a better way to test for setting default
# environment values
SS.env = 'development'
configurator.configure()
expect(SS.config.pack_assets).toEqual false
SS.env = 'production'
configurator.configure()
expect(SS.config.throw_errors).toEqual false
expect(SS.config.log.level).toEqual 0
expect(SS.config.client.log.level).toEqual 0

# These tests ensure that the application config overrides
# the default config, and that the environment config overrides
# the application config.
#
# NOTE - It would be nice to use asynchronous tests in place of calling synchronous functions.
describe "merging config files", ->

beforeEach ->
fs.mkdirSync 'lab', mode
fs.mkdirSync 'lab/config', mode
fs.writeFileSync 'lab/config/app.json', '{"client":{"log":{"level":1}}}'
SS.root = "lab"
SS.env = 'development'

afterEach ->
fs.unlinkSync 'lab/config/app.json', mode
fs.rmdirSync 'lab/config', mode
fs.rmdirSync 'lab', mode

it "should set the SS.config application values from the /config/app.json file, if it exists", ->
configurator.configure()
expect(SS.config.client.log.level).toEqual 1

it "should set the SS.config application values from the environment config file, if it exists", ->
# Setup
fs.mkdirSync 'lab/config/environments', mode
fs.writeFileSync 'lab/config/environments/development.json', '{"client":{"log":{"level":2}}}'

configurator.configure()
expect(SS.config.client.log.level).toEqual 2

# Teardown
fs.unlinkSync 'lab/config/environments/development.json', mode
fs.rmdirSync 'lab/config/environments', mode

0 comments on commit 4ce7caf

Please sign in to comment.