using System.Collections.Generic;
public static void Main()
new UnitTests().PerformUnitTest();
public void PerformUnitTest()
var mockClient = new Mock<IHttpClient>();
.Setup(mc => mc.Get<List<string>>(It.IsAny<string>()))
.Returns(new List<string>
var mockRepo = new Mock<ICustomerRepository>();
.Setup(mr => mr.GetCustomer(It.Is<int>(i => i == 1)))
.Setup(mr => mr.GetCustomer(It.Is<int>(i => i == 2)))
var mockProfile = new Mock<IProfileService>();
.Setup(mp => mp.GetPropertyValue(It.Is<string>(s => s.Equals("CustomerId1", StringComparison.OrdinalIgnoreCase))))
.Setup(mp => mp.GetPropertyValue(It.Is<string>(s => s.Equals("CustomerId2", StringComparison.OrdinalIgnoreCase))))
var sut = new CustomerValidator(mockClient.Object, mockProfile.Object, mockRepo.Object);
var actual = sut.GetCustomerAndValidateZip("CustomerId1");
var result = expected == actual ? "PASS" : "FAIL";
Console.WriteLine(result);
actual = sut.GetCustomerAndValidateZip("CustomerId2");
result = expected == actual ? "PASS" : "FAIL";
Console.WriteLine(result);
public class CustomerValidator
private readonly IHttpClient _client;
private readonly IProfileService _profile;
private readonly ICustomerRepository _repo;
public CustomerValidator(IHttpClient client, IProfileService profile, ICustomerRepository repo)
public bool GetCustomerAndValidateZip(string propertyName = "CustomerId")
int customerId = (int)_profile.GetPropertyValue(propertyName);
var customer = _repo.GetCustomer(customerId);
var allZipCodes = GetAllValidZipCodes();
return allZipCodes.Contains(customer.ZipCode);
private List<string> GetAllValidZipCodes()
return _client.Get<List<string>>("http://somezipcodeapi.com/GetAll");
public class ProfileService : IProfileService
public object GetPropertyValue(string propertyName)
return HttpContext.Current.Profile.GetPropertyValue(propertyName);
public interface IProfileService
object GetPropertyValue(string propertyName);
public int ID { get; set; }
public string ZipCode { get; set; }
public class CustomerRepository : ICustomerRepository
public Customer GetCustomer(int customerId)
var db = new DbContext();
return db.Customers.SingleOrDefault(i => i.ID == customerId);
public interface ICustomerRepository
Customer GetCustomer(int customerId);
public List<Customer> Customers { get; set; }
public class HttpClient : IHttpClient
public T Get<T>(string url)
public interface IHttpClient