Cant use SPSite on unit test projects?

Posted on 2011-04-27
Last Modified: 2012-05-11
on my webpart, I have this


private void GetRowCount()
      IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();

      IListsHelper serviceList = serviceLocator.GetInstance<IListsHelper>();

      string query = "<Where><Eq><FieldRef Name='Vendedor'/>" +
          "<Value Type='Text'>Luis</Value></Eq></Where>";

      int x = serviceList.GetListRowCount("Sales Marketing", SPContext.Current.Site.Url, query);



It works perfect


but on a unit test project, I hand typed the url like this

    public void TestRowCount()
      // In this section we set up the SharePoint Service Locator. We replace the default current service locator with a new instance
      // of the ActivatingServiceLocator class. We register our mock classes as the default implementations of ILists.
      // Finally we use the service locator to instantiate our mock classes
      ActivatingServiceLocator replaceLocator = new ActivatingServiceLocator();
      replaceLocator.RegisterTypeMapping<IListsHelper, MockLists>(InstantiationType.AsSingleton);

      list = SharePointServiceLocator.GetCurrent().GetInstance<IListsHelper>() as MockLists;      
      ListsHelper mockList = new ListsHelper();

      // In this section we perform the action that we want to test. We create a new instance of Lists
      // and we call the GetListRowCount method.
      string query = "<Where><Eq><FieldRef Name='Vendedor'/>" +
         "<Value Type='Text'>Luis</Value></Eq></Where>";

      int result = mockList.GetListRowCount("Sales Marketing", "http://w2k8-wss2010", query);

      //In this section we use various assert statements to verify that the Lists class behaved as expected


I debugged first the webpart to see the URL, and its exactly the same I typed to make the test.

It says FILENotFoundException, not webapplication has been found at that url??


makes no sense

Question by:Simbios
    1 Comment
    LVL 14

    Accepted Solution

    I think the problem is in this line of code

    You should create a new instance of the of the service locator using the cast (replaceLocator)

    Give that a try

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I recently came across an issue with a MOSS 2007 deployment where access into some sub-sites were denied, even for the MOSS farm administrators. A bit of background to the setup of this MOSS farm; this was a three server setup, consisting of a fr…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now