1. Welcome
  2. Getting Started [+/-]
    1. Installing from RubyGems
    2. Installing from GitHub.com
    3. Installing with Sake
  3. The Basics [+/-]
    1. Hello World
      1. Application Generation Options
    2. Paths
    3. Environments
    4. Rake
  4. Configuration [+/-]
    1. Configatron
    2. Initializers
      1. Gems
      2. Mime-Types
  5. Routing [+/-]
    1. Default Routes
    2. Named Routes
    3. RESTful/Resource Routes
    4. Nested Resource Routes
    5. Regex Routes
    6. Wildcard Routes
    7. Blocks in Routes
    8. Redirecting
    9. Error Handling
    10. Deferred? Routes
    11. Misc. Routing
  6. Controllers [+/-]
    1. Actions
    2. Headers/Status
    3. Handling Content Types
    4. Filters
    5. Helpers
    6. Layouts
    7. Tell Messaging
    8. Redirecting
    9. Rendering
      1. Engines
        1. Erubis
        2. XML Builder
        3. Extending
          1. Case Study: PDF Writer
      2. Types
        1. :action
        2. :text
        3. :inline
        4. :xml
        5. :url
        6. :partial
        7. :template
        8. :public
        9. Extending
          1. Case Study: PDF Writer
  7. Views [+/-]
    1. Helpers
    2. Layouts
    3. Assets Host
    4. Assets Management
    5. Form Builders
  8. Sessions [+/-]
    1. Session Store API
  9. Request/Response [+/-]
    1. Cookies
  10. Testing [+/-]
    1. RSpec
    2. Test::Unit::TestCase
  11. Porlets [+/-]
    1. Developing
    2. Testing
    3. Packaging
    4. Using
  12. Plugins [+/-]
    1. Extending
      1. Case Study: PDF Writer
  13. Deploying [+/-]
    1. Thin
    2. Passenger (mod_rails)
    3. Joyent Accelerator
  14. Mack More [+/-]
    1. mack-active_record
    2. mack-asset_packager
    3. mack-caching
    4. mack-data_factory
    5. mack-data_mapper
    6. mack-distributed
    7. mack-encryption
    8. mack-facets
    9. mack-haml
    10. mack-javascript
    11. mack-localization
    12. mack-markaby
    13. mack-notifier
    14. mack-orm
    15. mack-pdf_writer
  15. Rails to Mack Cheat Sheet
  16. Contributing
  17. APIs [+/-]

Testing Mack Application using RSpec

As described in the introduction to testing, all Mack applications will use RSpec as its testing framework unless specified to use Test::Unit::TestCase. This chapter will talk about what's involved in writing RSpec tests in the Mack Framework and how it integrates with mack-orm.

If you haven't done so, let's create a Mack application by calling $ mack foo, which will generate a Mack application named foo and use the RSpec as its test framework. Now, let's take a peek inside the foo's folder; you'll notice that there's a folder named test, and in it, there are a few other folders: controllers, helpers, helpers/controllers, helpers/controllers, helpers/views, models. And in them are actual test files.

All these files are generated, because by default all Mack applications contain a default controller and view, and with it the test files also got generated. Part of the reason is to ensure that newly generated app doesn't contain errors, and the other reason is so that developer like you can look at the test files as a reference to build your own test suites.

If you have never used RSpec before, then it'd be to your best interest to learn more about RSpec at http://rspec.info

If you look at the tests generated by Mack, you'll notice a pattern on how the tests are structured: First, at the top of the file, we require the spec_helper.rb file located at the root of the test folder. The spec_helper is the one that sets up the environment of the test, and also require any files that may be needed by the test.

You'll also notice that there's a file called spec.opts file in the root test folder. This file define the options passed in the spec command. You'll never have to explicitly use this file, because the rake command will take care of it.

As you have noticed, the rake command will run the test entire test suites of your application. How can you run individual test suite in your application? Or to go even further, how can I run individual test inside a test suite? Let's look at the following example on how to achieve those two things:

# if you run the individual test frequently, I'd suggest you setup an alias for rspec
# so you don't need to type in the formatting options all the time
# set alias for rspec to be 'spec --format specdoc --colour'.

# now, in your application folder, let's say you want to run the default_controller_spec.rb file
$ spec test/controllers/default_controller_spec.rb

# now here's how to run the individual test:
$ spec test/controllers/default_controller_spec.rb -e "DefaultController should be able to successfully connect to homepage"

# Note that the rspec test is structured like a tree: [top_level_describe] -> [example_groups].  
# And each describe can have another layer of describe.  So in order to run the individual test
# you will need to match the tree structure as stated in the above example.

One more important thing. If you use ORM support in Mack (either mack-data_mapper or mack-active_record), you'll get transactional support automatically. So each DB transaction will get rolled back after each test.

This guide was written for Mack version 0.8.2