2017-04-27 135 views
1

嘿,我从来没有用过Jasmine,但我需要这个小工程,我正在努力。不能完全弄清楚,任何帮助将不胜感激。我看了各种教程并讨论了这个问题,但是对于这个新手并没有什么帮助。

JS源文件

> $(document).ready(function(){ 
>  
> 
> $.ajax({ type: 'GET', dataType: "json", url: "db.php/realmadrids", 
> success: showResponse, error: showError }); 
> 
> 
> 
> 
> 
> 
> console.debug("error"); function showResponse(responseData){ 
> 
> //$("#get1").click(function getPlayers(responseData) { 
> console.log('Image is clicked'); console.log(responseData); 
>  $.each(responseData.realmadrid, function(index, realmadrid){ 
>  console.log(' is '); 
>   $("#playercontentRealMadrid").append("</br><strong> Full Name: </strong>" +realmadrid.PlayerName+ " "+realmadrid.PlayerLastName+" 
> </br> <strong>Player Position: </strong>" +realmadrid.PlayerPosition+" 
> </br><strong>Player Age: </strong>" +realmadrid.Age+" 
> </br><strong>Player Height: </strong>" +realmadrid.Height+" 
> </br><strong>Player Weight: </strong>" +realmadrid.Weight+" 
> </br><strong>Team Name: </strong>" +realmadrid.TeamName+"</br>"); 
>  
>   console.log('Data should output'); // }); }); 
> 
> console.log(responseData); 
>  } 
>  console.debug("hello"); 
> 
> function showError(){ alert("Sorry, but something went wrong. Fix 
> it!!!!") } 
> 
> }); 

这里是我的测试代码:

//Test Suite 
describe("Spy on my own AJAX call", function(){ 

    it("should make AJAX request with successful setting", function() { 

     spyOn($, "ajax"); 

     expect($.ajax).toHaveBeenCalledWith({ 
      url:'db.php/realmadrids', 
      type:'GET', 
      dataType: "json", 
      sucess: showResponse, 
      error: showError 
     }); 

    }); 

}); 

回答

0

您需要包括jQuery的。

在这种情况下$实际上是一个函数(来自jQuery库),并且因为jQuery没有被加载,所以你得到的错误是这个函数没有被定义。

以与包含Jasmine库相同的方式包含jQuery。一种方法是使用CDN:

<script src="https://code.jquery.com/jquery-1.11.3.js"></script> 
+1

它总是那些小事情,谢谢! –