Cant use SPSite on unit test projects?

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

Who is Participating?
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.