Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rhildred/qunitintro
simpler qUint examples
https://github.com/rhildred/qunitintro
Last synced: 14 days ago
JSON representation
simpler qUint examples
- Host: GitHub
- URL: https://github.com/rhildred/qunitintro
- Owner: rhildred
- Created: 2016-03-07T18:35:28.000Z (almost 9 years ago)
- Default Branch: gh-pages
- Last Pushed: 2021-08-06T12:41:23.000Z (over 3 years ago)
- Last Synced: 2024-04-14T14:03:15.004Z (9 months ago)
- Language: HTML
- Size: 8.79 KB
- Stars: 0
- Watchers: 3
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# [qunitIntro](https://github.com/rhildred/qunitIntro)
## Simple qunit examples, for Mobile class.
Qunit is also a strong framework for doing test driven development. In this example, we also look at Mocks, a powerful way of isolating a javascript unit from it's backend service.
### [Basic Unit Test](https://rhildred.github.io/qUnitIntro/test1.html)
```
QUnit basic example
QUnit.test( "a basic test example", function( assert ) {
var value = "hello";
assert.equal( value, "hello", "We expect value to be hello" );
});
```
A QUnit test contains at least 1 assertion. In this case `assert.equal( value, "hello", "We expect value to be hello" );`### Most things in Javascript depend on the [observer pattern](https://rhildred.github.io/qUnitIntro/test2.html)
```
QUnit async example
QUnit.test( "a test of 2 async tests example", function( assert ) {
assert.expect( 2 );
var done1 = assert.async();
var done2 = assert.async();
setTimeout(function() {
assert.ok( true, "test resumed from async operation 1!" );
done1();
}, 500 );
setTimeout(function() {
assert.ok( true, "test resumed from async operation 2!" );
done2();
}, 150);
});
```
`assert.expect` and `assert.async` allow us to know when our observations are complete.
### [.ajax for api testing](https://rhildred.github.io/qUnitIntro/firebasetest.html)
```
QUnit Ajax example
QUnit.test( "test of new firebase url", function( assert ) {
assert.expect( 2 );
var done = assert.async();
var done2 = assert.async();
jQuery.ajax({
url: "https://dazzling-heat-1553.firebaseio.com/tests.json"
}).done(function(data){
assert.ok(data != null, JSON.stringify(data));
done();
}).fail(function(err){
assert.ok( false, JSON.stringify(err) );
done();
});
jQuery.ajax({
url: "https://dazzling-heat-1553.firebaseio.com/tests2.json"
}).done(function(data){
assert.ok(data != null, JSON.stringify(data));
done2();
}).fail(function(err){
assert.ok( false, JSON.stringify(err) );
done2();
});
});
```
The above is a failing test until we add to firebase:
```
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"tests":{
".read":true
}}
}```
Perhaps just out of habit, I like to do my api testing with jQuery.ajax. This is an example of testing my endpoint on the firebase api. Note the use of `assert.expect` and `assert.async`.
### [Mockjax mock](https://rhildred.github.io/qUnitIntro/mockjax.html)
```
QUnit mockjax example
jQuery.mockjax({
url: "/restful/fortune",
responseText: {
status: "success",
fortune: "Are you a mock turtle?"
}
});
QUnit.test( "test of mockjax url", function( assert ) {
assert.expect( 1 );
var done = assert.async();
jQuery.ajax({
url: "/restful/fortune"
}).done(function(data){
assert.ok(data != null, JSON.stringify(data));
done();
}).fail(function(err){
assert.ok( false, JSON.stringify(err) );
done();
});
});
```
This example is just like the previous except for:
```
jQuery.mockjax({
url: "/restful/fortune",
responseText: {
status: "success",
fortune: "Are you a mock turtle?"
}
});```
The mock, "stubs out" an api call allowing us to test client code independent of the api. This allows the client team to work seperately from the api team, or without internet as the case may be.