How can I load data using $.get during tests?

Apr 16, 2012 at 3:28 PM

Hi,

I want to be able to use the $.get method to retrieve data from the server. How can I do this in both browser and phantom mode?

is there a config file I can create to point them to a host URL instead of a temp location?

Thanks.

Coordinator
Apr 16, 2012 at 3:48 PM

What is the behavior you are currently seeing when you try to use $.get?

I will usually stub out the behavior of the Ajax calls in my unit tests to make them faster and more dependable.

 

-Matt

Apr 16, 2012 at 4:18 PM
Edited Apr 16, 2012 at 4:19 PM

 

Here is the code:

test("Can fetch rows", function () {

    stop();

    $.get('someUrl',
    {
        orderBy: "someField",
        skip: 0,
        take: 10
    },
            function (recvData) {

                start();
                ok(recvData != "");
  
            });


});

The success callback gets called but the recvData is ""

Coordinator
Apr 16, 2012 at 5:04 PM

What is "someUrl"? 

I looked into this replacing "someUrl" with "http://www.google.com" and I did notice that didn't work. After digging a little deeper I realized that it doesn't work since there is a setting localToRemoteUrlAccessEnabled which is default set to false. This will block the ajax request to a remote site. This makes some sense since in a browser unless the test page is running under the remote urls domain this ajax call will fail because of same-origin policy.

However, if setting this to true will help I can make the change. But it may be easier just to stub how to get call.

Apr 16, 2012 at 6:09 PM

Wouldn't it work to use jasmine-ajax (https://github.com/pivotal/jasmine-ajax) to mock the ajax call?

Apr 17, 2012 at 11:19 PM

Thanks for your replies.

"someurl" is a relative url on my site that returns rows of data in json.

 For now I have mocked the $.get with:

 

var origJQueryGet = $.get;

function setup() {
    
    $.get = function (options) {
        var fakeData = new returnFakeData()
        options.success(fakeData); 
    };

};

function tearDown() {
    $.get = origJQueryGet;
};

It would be cool to be able access localToRemoteUrlAccessEnabled. May need the web-security setting too?

Aug 1, 2012 at 9:55 PM

Hi, I am having the same problem. I am writing integration tests and uses a remote url. Is is possible to set the localToRemoteUrlAccessEnabled to true? Or add another parameter in the Chutzpah TestRunner to accept bool if the user wants it enabled or not?

 

 Thanks.

Coordinator
Aug 1, 2012 at 9:59 PM

I made this change in the lastest version of Chutzpah. Can you verify you are using the 2.0.1 version?

Aug 2, 2012 at 3:54 PM

I just got the latest and fixed all the references. Now I am getting a different error.

 

System.IO.FileLoadException: Could not load file or assembly 'chutzpah, Version=2.0.1.140, Culture=neutral, PublicKeyToken=1ca802c37ffe1896' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Coordinator
Aug 2, 2012 at 3:58 PM

Are you sure you updated all the files? Can you delete the Chutzpah folder and copy fresh?

 

Also, are you using nuget or just downloading the zip?

Aug 2, 2012 at 4:04 PM

Ok, I'll get a fresh copy. And yes, I just downloaded the zip.

 

Below is the list of files that I got:

1. JSRunners (3 js files)

2. TestFiles with 2 subfolders

3. chutzpah.console.exe v.2.0.1.140

4. chutzpah.dll v.2.0.1.140

5. phantomjs.exe

6. ServiceStack.Text.dll v.3.8.3.0

7. StructureMap.dll 2.6.2.0

 

 

Coordinator
Aug 2, 2012 at 7:43 PM

ok, After getting a fresh copy in a fresh directory did you have the same issue? Can you verify the dll's and exe's are unblocked?


Aug 2, 2012 at 9:14 PM
Edited Aug 2, 2012 at 9:15 PM

Ok, I was able to get a fresh, clean copy. Now, I am getting a Chutzpah.TimeoutException. I created a test runner that uses Chutzpah so we can run it on continuous integration. 

Stack Trace:

Chutzpah Error: Chutzpah.Exceptions.ChutzpahTimeoutException: Timeout occured when running D:\Project\APD-Platform\NGP\Dev\2012\NGE\Source\ProjectSolutions\TestResults\kl11297_KL11297 2012-08-02 14_07_51\Out\RepairLineIntegrationTests\AddRepairLineTests(REST).htm   at Chutzpah.TestRunner.HandleTestProcessExitCode(Int32 exitCode, String inputTestFile) in d:\Dev\Chutzpah\Chutzpah\TestRunner.cs:line 191   at Chutzpah.TestRunner.InvokeTestRunner(String headlessBrowserPath, TestOptions options, TestContext testContext, TestRunnerMode testRunnerMode, ITestMethodRunnerCallback callback) in d:\Dev\Chutzpah\Chutzpah\TestRunner.cs:line 176   at Chutzpah.TestRunner.<>c__DisplayClass2.<ProcessTestPaths>b__1(String testFile) in d:\Dev\Chutzpah\Chutzpah\TestRunner.cs:line 133 While Running:D:\Project\APD-Platform\NGP\Dev\2012\NGE\Source\ProjectSolutions\TestResults\kl11297_KL11297 2012-08-02 14_07_51\Out\RepairLineIntegrationTests\AddRepairLineTests(REST).htm

 

Code:

	    
	    var testResultsSummary = (TestCaseSummary)null;
 
            try
            {
                var testRunner = TestRunner.Create(true);
                var runnerCallback = (RunnerCallback)new StandardConsoleRunnerCallback(true);
                
                var options = new TestOptions
                                  {
                                      OpenInBrowser = false,
                                      TestFileTimeoutMilliseconds = 30000
                                  };
 
                testResultsSummary = testRunner.RunTests(_testFile, options, runnerCallback);
            }
            catch (Exception ex)
            {
                Assert.Fail("Running " + _testDescription + " test(s) failed with fatal exception: \r\n" + ex + "\r\n" + ex.StackTrace);
            }
            finally
            {
                if (!testResultsSummary.Tests.Any())
                {
                    Assert.Fail("Running " + _testDescription + " test(s) failed with an unknown error, Chutzpah result returned was empty."); //failing here
                }
                else if (testResultsSummary.FailedCount > 0)
                {
                    Assert.Fail("Running " + _testDescription + " test(s) failed with following errors: \r\n");
                }
                else
                {
                    Assert.IsTrue(true"Running " + _testDescription + " test(s) succeeded");
                }
            }

 

 

Sorry, I am new to Chutzpah and having lots of problems. Thanks in advance.

Coordinator
Aug 2, 2012 at 10:05 PM

Could you give me an example test file that reproduces this issue?


Aug 2, 2012 at 10:16 PM
Edited Aug 2, 2012 at 10:18 PM

A have a test html page that has all the script references including the test js (please see below). The html page is called on a cs file which calls the test runner above:

 

new JsIntegrationTest("test.htm").Run();

 

 

test.js:

 

 

module("TestName",
{
    setup: function () {
        //some setup
    },

    teardown: function () {
        //some teardown
    }
});

test("TestName", function () {
    var collection = new SomeCollection([], { options: options }); //SomeCollection has a secured url something likec ollection.url = "https://someUrlForRestCall";

    collection.bind("reset", function () {
       //do some assertions

        start();
    }, this);

   collection.someMethod(); //will trigger the rest call
    stop();
});
Coordinator
Aug 2, 2012 at 10:20 PM

Does your JS code actually make an HTTPS web call? I have seen some issues before with making HTTPS calls that I still need to look into. 

One option is to stub out the HTTP request, this also has the benefit of making tests faster and less fragile.

Aug 2, 2012 at 10:28 PM

It does. When I run the tests standalone (from the test.html), it works fine. All my tests are passing. I'm just getting the timeout exception when I run my tests from my test runner. 

I really appreciate your help.

Feb 6, 2013 at 11:51 AM
Sorry to jump into an existing discussion, but I would like this too (localToRemoteUrlAccessEnabled =true), but cannot figure out where to activate that setting. Im using the 2012 integrated testrunner.
Thanks
Coordinator
Feb 7, 2013 at 4:47 PM