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

 [TestMethod]
    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();
      SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator);
      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
      Assert.AreEqual(2,result);

      SharePointServiceLocator.Reset();  
    }

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

 
SimbiosAsked:
Who is Participating?
 
GeorgeGerguesCommented:
I think the problem is in this line of code
SharePointServiceLocator.ReplaceCurrentServiceLocator(replaceLocator);

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

Give that a try
0
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.