我在如何在易於測試的MVC站點中部署WCF客戶端存在嚴重問題。我努力在沒有實際訪問端點的情況下設置服務的模擬。單元測試模擬WCF客戶端
網站示例控制器測試
public class ProfileController : ControllerExtended
{
public ProfileController(IUserService membershipService, IDropDownService dropdownService, ISiteService siteService)
{
WCFService.Instance.Client = siteService; //Should maybe be a serpate service.
_membershipService = membershipService;
_dropDownService = dropdownService;
_siteService = siteService;
}
public ActionResult Index()
{
UserComp profile = _siteService.ProfileGet(_sharedContext.CurrentUser.id);
return View(new ProfileViewModel { Profile = profile });
}
}
WCF辛格爾頓(我想我的實現WCF的是我的問題,它應該是在一個界面?)
public sealed class WCFService
{
public SiteServiceI Client { get; set; }
#region Singleton
static readonly WCFService query = new WCFService();
static WCFService()
{
}
WCFService()
{
}
public static WCFService Instance
{
get { return query; }
}
#endregion
}
單元測試
[TestFixture]
public class UnitTest1
{
private Mock<SiteService> mockSiteService;
private Mock<IUserService> mockMembershipService;
private Mock<IDropDownService> mockDropDown;
private Mock<SiteServiceIClient> mockServiceClient; //new Mock<SiteServiceIClient>();
//private Mock<WebService> mockWebService;
[SetUp]
public void SetUp()
{
mockSiteService = new Mock<ISiteService>();
mockMembershipService = new Mock<IUserService>();
mockDropDown = new Mock<IDropDownService>();
mockServiceClient = new Mock<SiteServiceIClient>();
mockWebService = new Mock<WebService>(mockServiceClient);
}
[Test]
public void CheckHomeIndex_Controller()
{
var controller = new HomeController(mockMembershipService.Object, mockDropDown.Object, mockPTSearch.Object, mockServiceClient.Object); // mockServiceClient times out.
Assert.AreEqual("this", "this");
}
}
我假設發生了錯誤,因爲單身人士在初始化後立即設置服務。我試圖實現一個單獨的服務純粹爲呼叫到WCF,但沒有成功實施它在一個可測試的方式。不太確定,如果這是模糊的,但我讀了很多,而不是更接近。
所以這樣我基本上需要添加整個WCF接口的副本本地副本?謝謝,我會繼續嘗試! – Oli
@Oli - 是的但不是,您在您創建的界面中實現了「本地副本」,但僅從該界面公開了WCF服務中對您和您的應用程序感興趣的操作。 –
目前這是第一個訪問該服務的應用程序,因此它是一對一的,因爲它們將是相同的,但是要感謝您的確認。 – Oli