From fe45e7441d125ef16e7fb8809b91dbde57a145fe Mon Sep 17 00:00:00 2001 From: Scott Walton Date: Wed, 13 Jul 2016 09:47:08 +0100 Subject: [PATCH] Added basic testing #21 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added the bare-bones of a testing guide. We’ll need to take this further and document how we actually set all this up. --- en/SUMMARY.md | 3 +- en/testing/README.md | 77 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 en/testing/README.md diff --git a/en/SUMMARY.md b/en/SUMMARY.md index 97133f2..0190e4d 100644 --- a/en/SUMMARY.md +++ b/en/SUMMARY.md @@ -24,8 +24,9 @@ * [Sharing common view Behavior](behaviors/README.md) * [Example Behaviors](behaviors/common_behaviors.md) * [Handling Data Latency](data_latency/README.md) -* [Dealing with Complex Persisted Data](persisted_data/README.md) +* [Handling Persisted Data](persisted_data/README.md) * [Nesting Your Views](layoutview/README.md) +* [Testing your Application](testing/README.md) * [Passing Data with Events](messaging/README.md) * [FAQ](faq/README.md) * [Cookbook](cookbook/README.md) diff --git a/en/testing/README.md b/en/testing/README.md new file mode 100644 index 0000000..f13951e --- /dev/null +++ b/en/testing/README.md @@ -0,0 +1,77 @@ +# Testing Marionette Applications + +When working with JavaScript, there are always multiple ways of testing your +application. In this guide, we will cover the basics of testing, different +strategies you can use to test your applications, and provide a simple test +framework configuration you can use to test your own apps. + +## What is Testing? + +A key part of building applications is ensuring they're fit for use before we +release them to our customers. Traditionally, we would manually test our +systems after building them. This process is very time-consuming and +error-prone, requiring previous bugs to be constantly re-tested lest regressions +creep back into our code. + +To improve this situation, the principles of test-driven development were +designed. This places a heavy emphasis on building up automated tests that can +be run against your code base constantly. Achieving a good coverage of your +application's use-cases and potential problem-areas helps ensure that you are +providing a high-quality system to your users at all times. + +## Testing a Model + +Let's look at a simple test case with a custom model method. We will use a basic +assertion framework: + +```javascript +var assert = require('assert'); +var ToDo = require('./models/todo'); + +describe('ToDo', function() { + var model; + + beforeEach(function() { + model = new ToDo(); + }); + + afterEach(function() { + model = null; + }); + + it('can save a note', function() { + model.makeNote('Scott', 'Get the shopping in for later'); + + assert.ok(model.get('created')); // Assert that created was set + }); +}); +``` + +Our `models/todo.js` file contains: + +```javascript +var Backbone = require('backbone'); +var moment = require('moment'); + +module.exports = Backbone.Model.extend({ + defaults: { + author: '', + text: '', + created: '' + }, + + makeNote: function(author, text) { + this.set({ + author: author, + text: text, + created: moment().format('YYYY-MM-DDThh:mm:ss') + }); + } +}) +``` + +This gives us a basic outline for testing our model methods behave as expected. + +## Configuring a Test Framework + +## When to Test