Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cant use SPSite on unit test projects?

Posted on 2011-04-27
1
Medium Priority
?
746 Views
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

 [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

 
0
Comment
Question by:Simbios
1 Comment
 
LVL 14

Accepted Solution

by:
GeorgeGergues earned 2000 total points
ID: 35493426
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

571 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