Link to home
Start Free TrialLog in
Avatar of Simbios
SimbiosFlag for Afghanistan

asked on

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

 
ASKER CERTIFIED SOLUTION
Avatar of GeorgeGergues
GeorgeGergues

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial