Backbone - 使用Jasmine测试一个简单的模型会导致TypeError(Backbone - testing a simple model using Jasmine results in TypeError)
我试图学习使用Jasmine测试基于Backbone的应用程序的方法。 为此,我从这里获取了一些示例代码: http : //msdn.microsoft.com/en-us/scriptjunkie/hh377172 。
Photo = new Backbone.Model.extend({ defaults:{ title: 'Another photo!', tags: ['untagged'], location: 'home', src: 'placeholder.jpg' }, initialize: function(){ console.log('this model has been initialized'); this.bind("change:title", function(){ var title = this.get("title"); console.log("My title has been changed to.." + title); }); }, setTitle: function(newTitle){ this.set({ title: newTitle }); } });
然后编写测试规范如下:
describe("Photo Model", function() { it("verifies title", function() { var myPhoto = new Photo(); myPhoto.set({ title: "On the beach" }); expect(myPhoto.get("title")) .toEqual("On the beach"); }); });
在运行它时,测试因TypeError而失败
TypeError: Object [object Object] has no method 'apply' at new <anonymous> (http://localhost:88/backbone/WebClient-Backbone2/js/backbone.js:1103:41) at [object Object].<anonymous> (http://localhost:88/backbone/WebClient-Backbone2/test/spec.js:28:16) at [object Object].execute (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1001:15) at [object Object].next_ (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1790:31) at [object Object].start (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1743:8) at [object Object].execute (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:2070:14) at [object Object].next_ (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1790:31) at [object Object].start (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1743:8) at [object Object].execute (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:2215:14) at [object Object].next_ (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1790:31)
I was trying to learn the ways of testing Backbone-based app using Jasmine. For this, I picked up some sample code from here: http://msdn.microsoft.com/en-us/scriptjunkie/hh377172.
Photo = new Backbone.Model.extend({ defaults:{ title: 'Another photo!', tags: ['untagged'], location: 'home', src: 'placeholder.jpg' }, initialize: function(){ console.log('this model has been initialized'); this.bind("change:title", function(){ var title = this.get("title"); console.log("My title has been changed to.." + title); }); }, setTitle: function(newTitle){ this.set({ title: newTitle }); } });
Then wrote the test spec as follows:
describe("Photo Model", function() { it("verifies title", function() { var myPhoto = new Photo(); myPhoto.set({ title: "On the beach" }); expect(myPhoto.get("title")) .toEqual("On the beach"); }); });
While running it, the test fails with TypeError
TypeError: Object [object Object] has no method 'apply' at new <anonymous> (http://localhost:88/backbone/WebClient-Backbone2/js/backbone.js:1103:41) at [object Object].<anonymous> (http://localhost:88/backbone/WebClient-Backbone2/test/spec.js:28:16) at [object Object].execute (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1001:15) at [object Object].next_ (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1790:31) at [object Object].start (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1743:8) at [object Object].execute (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:2070:14) at [object Object].next_ (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1790:31) at [object Object].start (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1743:8) at [object Object].execute (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:2215:14) at [object Object].next_ (http://localhost:88/backbone/WebClient-Backbone2/test/lib/jasmine/jasmine.js:1790:31)
原文:https://stackoverflow.com/questions/8543315
最满意答案
除了改变你的
==
(严格平等)之外并不多。 但是会与正常的平等一起工作。不知道你做错了什么,因为这段代码工作正常。
我会做的是拥有像这样的JSON响应
"{ error: true, }"
"{ error: false, data: yourDataSentByTheServer! }"
这样你只需要检查JSON响应中是否有错误属性。function stringType(){ const responseData = 'hello'; if(typeof(responseData) === 'string') { console.log('got api call ' + responseData); alert('test = ' + responseData); } else { console.log('got api call ' + responseData); alert(responseData); } } function objType(){ const responseData = { 1:'hello', 2: 'koko' } if(typeof(responseData) === 'string') { console.log('got api call ' + responseData); alert('test = ' + responseData); } else { console.log('got api call ' + responseData); alert(responseData); } }
<button onClick="stringType();">Click me for string</button> <button onClick="objType();">Click me for obj</button>
Other than changing your
==
for===
(strict equality) not much. But will work with normal equality.Dont know what you're doing wrong, because this code works just fine.
What I would do instead is having a JSON response like so:
"{ error: true, }" "{ error: false, data: yourDataSentByTheServer! }"
That way you only need to check if there's an error property inside your JSON response.
function stringType(){ const responseData = 'hello'; if(typeof(responseData) === 'string') { console.log('got api call ' + responseData); alert('test = ' + responseData); } else { console.log('got api call ' + responseData); alert(responseData); } } function objType(){ const responseData = { 1:'hello', 2: 'koko' } if(typeof(responseData) === 'string') { console.log('got api call ' + responseData); alert('test = ' + responseData); } else { console.log('got api call ' + responseData); alert(responseData); } }
<button onClick="stringType();">Click me for string</button> <button onClick="objType();">Click me for obj</button>
相关问答
更多-
检查响应消息的内容类型。 if (response.Content.Headers.ContentType.MediaType == "application/json") { // parse json } else { // parse xml } 您还可以从响应中读取第一个字符。 如果它是XML内容,您应该找到< 。 即使XML声明存在与否。 Check the content type of the response message. if (response.Content.He ...
-
你要做的是在json响应中对movies数组使用Array.prototype.find()并检查值 function getMoviesFromApiAsync() { return fetch('https://facebook.github.io/react-native/movies.json') .then((response) => response.json()) .then((responseJson) => { return resp ...
-
nodejs-将json对象转换为$ .get响应中的字符串数组(nodejs-convert json object to string array in $.get response)[2024-02-05]
通常,jQuery已经为您解析了JSON,尤其是在正确设置了内容类型标头的情况下。 我的猜测是你正在尝试解码已解码的对象。 尝试在$.parseJSON调用之前console.log("GET results: type: %s data: %s", typeof data, data) 。 Generally jQuery will already have parsed the JSON for you, especially if the content type header is set corr ... -
使用您的代码当前的方式将无法正常工作,因为错误消息直接指示: Uncaught InvalidStateError:无法从'XMLHttpRequest'读取'responseText'属性:只有当对象的'responseType'为''或'text'('json')时才能访问该值。 正如您可以看到以下证据: var xhr = new XMLHttpRequest(); //xhr.responseType = "json"; xhr.open("POST", "/echo/json/"); xhr.s ...
-
除了改变你的== (严格平等)之外并不多。 但是会与正常的平等一起工作。 不知道你做错了什么,因为这段代码工作正常。 我会做的是拥有像这样的JSON响应 "{ error: true, }" "{ error: false, data: yourDataSentByTheServer! }"这样你只需要检查JSON响应中是否有错误属性。 function stringType(){ const responseData = 'hello'; if(typeof(responseData) === ...
-
好的,问题是有时你返回的数据代表一个JSON对象,有时它代表一个JSON数组。 您的Android代码在所有情况下都将数据视为数组。 您可以处理此问题,但不能使用您现在使用的JSON.org库。 由于您正在为Android开发,因此您可以内置访问Gson库 - 并且可以使用它来执行此操作: JSONElement jsonElem = new JsonParser().parse(result); if(jsonElem.isJsonArray()) { // Normal data } else ...
-
Volley是否具有JSON参数,字符串响应原生支持(Does Volley have JSON Argument, String response native support)[2022-09-17]
final JSONObject body = new JSONObject(); try { // Todo: populate JSON body } catch (JSONException e) { e.printStackTrace(); } StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://...", n ... -
Bash:检查json响应并在字符串存在时写入文件(Bash: Check json response and write to a file if string exists)[2022-02-25]
一个理智的实现可能看起来像这样: retries=10 instance=server1 response_file=filename # define a function, since you want to run this code multiple times # the old version only ran curl once and reused that result fetch() { curl -sk https://my-app-api.com; } instance_info ... -
您所要做的就是在JavaScript中使用JSON.parse()。 promise.then( function(response) { response = JSON.parse(response); console.log("Success!", response); } 无论您从后端发送什么,您的responseGet函数都不会返回已解析的对象。 All you have to do is using JSON.parse() in your JavaScript. promis ...
-
function checkForValue(json, value) { for (key in json) { if (typeof (json[key]) === "object") { return checkForValue(json[key], value); } else if (json[key] === value) { return true; } } return f ...