{"action":"opened","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/750","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/750/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/750/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/750/events","html_url":"https://github.com/tgriesser/bookshelf/issues/750","id":77347872,"number":750,"title":"How to stub Model.parse() for testing insert?","user":{"login":"robwilkerson","id":7735,"avatar_url":"https://avatars.githubusercontent.com/u/7735?v=3","gravatar_id":"","url":"https://api.github.com/users/robwilkerson","html_url":"https://github.com/robwilkerson","followers_url":"https://api.github.com/users/robwilkerson/followers","following_url":"https://api.github.com/users/robwilkerson/following{/other_user}","gists_url":"https://api.github.com/users/robwilkerson/gists{/gist_id}","starred_url":"https://api.github.com/users/robwilkerson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robwilkerson/subscriptions","organizations_url":"https://api.github.com/users/robwilkerson/orgs","repos_url":"https://api.github.com/users/robwilkerson/repos","events_url":"https://api.github.com/users/robwilkerson/events{/privacy}","received_events_url":"https://api.github.com/users/robwilkerson/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-05-17T16:52:14Z","updated_at":"2015-05-17T16:52:14Z","closed_at":null,"body":"I have a model on which I'm storing a few ancillary data fields as JSON strings. When inserted, `Model.format()` ensures the fields are stringified and there's a corollary `Model.parse()` that ensures the fields are parsed/expanded when fetched.\r\n\r\nI'm trying to write Mocha unit tests to ensure that this functionality is behaving as expected, but I'm having a bit of trouble. My approach is pretty simple:\r\n\r\n1. Call the URL that creates the new record.\r\n1. Once the request ends, fetch the newly created record.\r\n\r\nOn that fetch, though, I need to prevent `Model.parse()` from doing anything so I can ensure that the properties are saved as strings. I'm doing something wrong that keeps this from working as expected. Although the values are getting saved properly when I inspect the database itself, the string values I anticipate coming back to me with the stubbed `Model.parse()` method are coming back fully parsed.\r\n\r\n    describe('#format', function() {\r\n        it('should stringify properties sent in as objects', function(done) {\r\n            MyModel.forge()\r\n                .save({\r\n                    user_id: '251a4210-9d51-4845-a585-876f32fc18e8', \r\n                    score: '759',\r\n                    first_name: null,\r\n                    last_name: null,\r\n                    phone: null,\r\n                    bio: null,\r\n                    flintstone_chars: [\r\n                        {\r\n                            fred: 'flintsone',\r\n                            wilma: 'flintstone'\r\n                        },\r\n                        {\r\n                            barney: 'rubble',\r\n                            betty: 'rubble',\r\n                        }\r\n                    ],\r\n                    references: [\r\n                        {\r\n                            humpty: 'dumpty',\r\n                            alice: 'wonderland',\r\n                        },\r\n                        {\r\n                            winston: 'churchill',\r\n                            barnaby: 'pickens',\r\n                        }\r\n                    ],\r\n                    questions: [\r\n                        {\r\n                            q: 'Why?',\r\n                            a: 'Because',\r\n                        },\r\n                        {\r\n                            q: 'Got milk?',\r\n                            a: 'I do not'\r\n                        }\r\n                    ]\r\n                })\r\n                .then(function(cr) {\r\n                    var mockCreditReport = CreditReport.forge(cr.toJSON());\r\n                    sinon.stub(mockCreditReport, 'parse');\r\n\r\n                    // Fetch the newly saved credit report\r\n                    mockCreditReport\r\n                        .fetch()\r\n                        .then(function(inserted) {\r\n                            // jshint expr:true\r\n\r\n                            inserted = inserted.toJSON({ shallow: true });\r\n                            inserted.should.have.property('flintstone_chars').and.be.a.String;\r\n                            inserted.should.have.property('references').and.be.a.String;\r\n                            inserted.should.have.property('questions').and.be.a.String;\r\n\r\n                            done();\r\n                        });\r\n                });\r\n        });\r\n    });"}}