Skip to content

Instantly share code, notes, and snippets.

@hotgazpacho
Created November 16, 2011 02:52
Show Gist options
  • Save hotgazpacho/1369116 to your computer and use it in GitHub Desktop.
Save hotgazpacho/1369116 to your computer and use it in GitHub Desktop.
WCF Service Test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ElmSelect.DomainModel;
using DataAccessLayer;
namespace WCFService
{
public class ElmResourcesService : IElmResourcesService
{
readonly IQueryable<School> _schools;
readonly IQueryable<Lender> _lenders;
public ElmResourcesService(IQueryable<School> schools, IQueryable<Lender> lenders)
{
_schools = schools;
_lenders = lenders;
}
public IList<string> GetSchools(string partialText)
{
return _schools.Where(x => x.Name.Contains(partialText)).Select(x => x.Name).ToList();
}
public School GetSchool(string schoolName)
{
return _schools.SingleOrDefault(x => x.Name == schoolName);
}
public IList<string> GetLenders(string partialText)
{
return _lenders.Where(x => x.Name.Contains(partialText)).Select(x => x.Name).ToList();
}
public Lender GetLender(string lenderName)
{
return _lenders.SingleOrDefault(x => x.Name == lenderName);
}
}
}
namespace WCFService.Test
{
class FakeSchools : List<School>
{
}
class FakeLenders : List<Lender>
{
}
[TestFixture]
public class WCFServiceTests
{
[Test]
public void GetSchools_PartialTextMatch_ListOfSchoolsWithPartialMatch()
{
//Arrange
var schools = new FakeSchools(new[]{new School("Ohio State University"), new School("Florida State University")});
var service = new WCFService(schools, null);
//Act
var result = service.GetSchools("Ohio");
//Assert
CollectionAssert.AreEquivalent(new[] {"Ohio State University"}, result)
}
}
}
@hotgazpacho
Copy link
Author

functionally equivalent to what you were doing, but without the potential NullReferenceExceptions from the Try/Finally stuff.

When you instantiate your ElmResourcesService, you give it 2 enumerations of strings. These can be anything from an array to a not-yet-materialized LINQ-to-SQL, EF, or NHibernate enumerable.

@hotgazpacho
Copy link
Author

technically, to compile, you probably need to instantiate new lists from the ILists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment