Permalink
Browse files

Update README.md

  • Loading branch information...
jphpsf committed Aug 29, 2012
1 parent 916baf8 commit 596097cb69184256161ab5b7ec24f2b9960bc359
Showing with 52 additions and 2 deletions.
  1. +52 −2 README.md
View
@@ -4,7 +4,54 @@
This project is a demo for the data provider pattern applied to Jasmine.
-It allows to DRY up Jasmine tests that needs to be run with multiple values.
+It allows to DRY up Jasmine tests that needs to be executed with multiple values.
+
+Imagine you have some validation code that must verify a username. A correct username matches the following requirement:
+
+ - Must be alphanumeric or underscore
+ - Minimum of 3 characters
+ - No more than 12 characters
+
+Positive and negative test cases could look like this:
+
+```javascript
+describe("username validation", function() {
+ it("should return true for valid usernames", function() {
+ expect(validateUserName("abc")).toBeTruthy();
+ expect(validateUserName("longusername")).toBeTruthy();
+ expect(validateUserName("john_doe")).toBeTruthy();
+ })
+
+ it("should return false for invalid usernames", function() {
+ expect(validateUserName("ab")).toBeFalsy();
+ expect(validateUserName("name_too_long")).toBeFalsy();
+ expect(validateUserName("no spaces")).toBeFalsy();
+ expect(validateUserName("inv*alid")).toBeFalsy();
+ })
+})
+```
+
+I don't really like to repeat many expectations within one test block. With the data provider pattern it would look like this:
+
+```javascript
+describe("username validation", function() {
+ using("valid values", ["abc", "longusername", "john_doe"], function(value){
+ it("should return true for valid usernames", function() {
+ expect(validateUserName(value)).toBeTruthy();
+ })
+ })
+
+ using("invalid values", ["ab", "name_too_long", "no spaces", "inv*alid"], function(value){
+ it("should return false for invalid usernames", function() {
+ expect(validateUserName(value)).toBeFalsy();
+ })
+ })
+})
+```
+
+## Code
+
+The following can be added to your spec helper to make this work:
```javascript
function using(name, values, func){
@@ -17,7 +64,10 @@ function using(name, values, func){
}
}
```
+## Demo
+
+See [demo hosted on GitHub](http://jphpsf.github.com/jasmine-data-provider/)
-# License
+## License
WTFPL

0 comments on commit 596097c

Please sign in to comment.