Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : Is there an established pattern for JMVC fixtures and their use with tests?

$
0
0
Yes, we often uses fixtures for tests, especially tests for widgets that use a model internally.

In a recent application, we created some high-level model tests that allow us to write a single test that tests against fixtures AND the service layer.

Here's the code:

  1. var adminLoggedIn,
  2. modules = {},
  3. currentModule;
  4. return {
  5. fixtureAndServiceModule: function(name, config){
  6. currentModule = { tests: [], config: config};
  7. modules[name] = currentModule;
  8. },
  9. fixtureAndServiceTest: function(name, fn){
  10. currentModule.tests.push({
  11. name: name,
  12. fn: fn
  13. })
  14. },
  15. createModulesAndTests: function(fixturesOnly){
  16. if(fixturesOnly !== true){
  17. for(var name in modules){
  18. var mod = modules[name]
  19. module(name+" services",mod.config);

  20. can.each(mod.tests, function(testData){
  21. test(testData.name, function(){
  22. fixture.on = false;
  23. testData.fn()
  24. })
  25. })
  26. }
  27. }
  28. for(var name in modules){
  29. var mod = modules[name]
  30. module(name+" fixtures",mod.config);

  31. can.each(mod.tests, function(testData){
  32. test(testData.name, testData.fn)
  33. })
  34. }
  35. },

What this code allows is a test like:


  1. helpers.fixtureAndServiceModule('safety/models/customer.js',{
  2. setup: function(){
  3. fixture.on = true
  4. fixtures.customerStore.reset()
  5. }
  6. })


  1. helpers.fixtureAndServiceTest("create and destroy",function(){
  2.   stop()
  3.   new Customer({
  4.     name: "TEST-Abbott"
  5.   }).save(function(customer){ ... }})

Basically, fixtureAndServiceTest creates a test that QUnit will run twice, once with fixtures on, another with them off. 

I'm not sure if that helps answer your question however.


Viewing all articles
Browse latest Browse all 3491

Trending Articles