Javascript Testing With JasmineBDD: Overriding Spy Return In Nested Describe

Posted about 2 years ago on April 30, 2011

I have been using Jasmine’s SpyOn method to stub results for dependencies within methods.

My goal was to override these stubbed methods in nested describe contexts. My first thought was to redefine the Spy (stub) using the spyOn method. This fails with an obvious error message: “Error: xyz has already been spied upon…”. What I needed to understand was that the spy object had already been created in the outer describe block, so I needed to simply re-use it. Instead of trying to re-declare the spy, just use the same object and use the andReturn() method to redefine the return value of the stubbed method.

Here is an illustration:

describe("Outer describe", function() {
    beforeEach(function() {
        //Setup Stuff
        spyOn(someObject, 'someMethod').andReturn(3);
    });

    it("...", function() {
        //Test here
    });

    describe("Inner Describe", function() {
        beforeEach(function() {
            //Nested setup stuff

            //Fails: "Error: someMethod has already been spied upon"
            spyOn(someObject, 'someMethod').andReturn(0);

            //Passes: The object has already been initialized as a spy, so use the existing spy object and tell it to change the return value.
            someObject.someMethod.andReturn(0);
        });

        it("...", function() {
            objectUnderTest.someMethod();
            expect($("input#some-element")).toBeDisabled();
        });
    });
});

ASP.Net MVC: Handling Forms Authentication Timeouts for AJAX Requests

Posted over 2 years ago on September 26, 2010

Problem: You need a way to redirect the user back to a login page when a user makes an AJAX request and their forms authentication session has timed out.

Solution: Add a HTTP header onto each AJAX response that indicates whether or not the user is still logged in and their session is active.

This is an example of how you might add a response header to each AJAX request.

using System.Web.Mvc;
using System.Web.Security;

namespace MVCAjaxFormsAuthTimeout.Helper
{
    public class CommunicateAuthStatusToAjaxClientAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if(filterContext.HttpContext.Request.IsAjaxRequest())
            {
                if(filterContext.HttpContext.User.Identity.IsAuthenticated)
                    filterContext.HttpContext.Response.Headers.Add("X_User_Logged_In", "true");
            }

            base.OnActionExecuted(filterContext);
        }
    }
}

Usage:

[CommunicateAuthStatusToAjaxClient]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }
    }

On the client side, I want every AJAX request made from jQuery to check this header.  Instead of touching every place where I make a call to the server, I use the ajaxSuccess(...) method to configure this once in the master page.

    
    

When the users' session has ended, the header variable will be empty and the alert and redirect will execute.

Source code is here.

Note: Modifying response headers requires "IIS Integrated Pipeline Mode" which the Cassini development server does not support.  Therefore, if you would like to see the code run you must configure IIS to handle the web project.

Rendering A Modal Dialog with ASP.Net MVC

Posted about 4 years ago on June 15, 2009

One of the most requested ‘Web 2.0’ features by users is the ability to pop up a “modal” dialog to the user.  There are a couple ways you could implement this functionality, but I’m going to show you a really easy way to do it with ASP.Net MVC.  (Hat tip to Jon Kruger for sharing this idea with me.)

Start with a very simple controller action:

controlleraction

Nothing spectacular going on here.  The “RandomModal” string points to a ASCX file that is in the views directory, and the second parameter is the model that you want the partial view to render:

ascx

And finally, on the view that is hosting the modal dialog there is a chunk of javascript that makes the call, throws the returned DOM elements into a parent div, and displays the div as a dialog:

javascriptcallingaction

Another place you could implement this pattern is on a grid.  A common request is to click on a row and view more detailed information about the record.  To implement this all you’d need to do is stash a row identifier on the html row, and pass that back to the controller on click.  Something like this:

postforrowdetail

The complete solution with this code can be downloaded here:

Content Not Being Indexed in Sitefinity

Posted over 4 years ago on March 05, 2009

This post will be a little esoteric.  I’m working on a Sitefinity project, and one of the features we wanted to implement was the built in site search module.

After configuring the search module everything appeared to be working properly, but after looking a little closer we realized that none of our site content was being indexed.  The only thing that was being indexed was each page’s meta tags.  I found out in one of the Sitefinity forums that you can control what gets indexed on the site by using an XML configuration file.  You can find your site’s configuration file in this path: /%AppDirectory%/App_Data/Search/%IndexName%/fieldsInfoProvider.xml

Here is what the file looks like when you set up a new index:

fieldsinfoproviderOrig

Here’s what the attributes mean:
name= This attribute is for the reader.  You can name it whatever you want.
weight= Gives weight to the content in the search results.  (Eg: Items weighted higher will return higher in the search results than lesser weighted items)
indexAttribute= Use this attribute to index all tags with a certain attribute.
filterTag= Use this to tell Sitefinity that you want to index by tag name. 
filterAttributes= Use this to filter the attributes by value.

In the site I am building, I want an entire HTML node to be indexed it happens to be:

<div id=”primary”></div>

So to add this content to my index, I added a field node in the fieldsInfoProvider.xml file as such:

 fieldsinfoprovider

If I wanted to add a HTML node by class, I could have changed the filterAttributes= attribute to “class:someCSSClass”.  If my HTML node was a <UL>, I’d change filterTag= “UL”.

Google Chrome “Feeling Lucky Search”

Posted over 4 years ago on February 22, 2009

If you want to do a “Feeling Lucky” search from Google Chrome post haste, I’ve got a great trick for you.  Check this out:

weeb

Fewer clicks, happier browsing.

Here’s how to do it:

1) Create a custom search engine in Chrome:
1

2) Add the search engine with these properties:
2
URL: http://www.google.com/search?q=%s&btnI=Im+Feeling+Lucky

3) Enjoy!!