如何通过Moq模拟wcf服务(由asmx转换)
我有 Silverligth 单元测试应用程序(.net 4.0),并且添加了对我的 WCF 服务(3.5)的引用
,这是我的代码
MyService.svc
<%@ServiceHost Language="VB" Service="MyServiceWS.Service1" %>
MyService.asmx
<System.Web.Services.WebService(Namespace:="http://schemas.mypage.co.uk/MyService")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ServiceContract(Namespace:="http://schemas.mypage.co.uk/MyService")> _
<ServiceKnownType(GetType(Member))> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
来自 reference.cs
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<MyServiceApp.MyServiceService.Service1>, MyServiceApp.MyServiceService.Service1 {
我的测试类:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace MyServiceApp.UnitTests
{
[TestClass]
public class ShowroomLogViewModelTests
{
[TestMethod]
public void ReloadShowroomLogs_IsBusyShouldBeFalse()
{
var mockedWebService = Mock<IService1>;
问题是:
IService1 cannot be resolved
我如何模拟我的wcf服务?
编辑:更近一步
var mockedWebService =new Mock<MyService.Service1>;
我已经更改为这个,现在正在编译,但是...... 我无法访问方法,例如,
mockedWebService.GetShowroomLogs();
这是一个大问题,因为,我需要将 ShowroomLogViewModel 处的属性 ServiceClient 更改为接口,但我不能,因为这样我的视图模型就无法使用 wcf 服务方法。
I have Silverligth Unit test Application (.net 4.0) and I've added reference to my WCF service(3.5)
And here are my codes
MyService.svc
<%@ServiceHost Language="VB" Service="MyServiceWS.Service1" %>
MyService.asmx
<System.Web.Services.WebService(Namespace:="http://schemas.mypage.co.uk/MyService")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ServiceContract(Namespace:="http://schemas.mypage.co.uk/MyService")> _
<ServiceKnownType(GetType(Member))> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
From reference.cs
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<MyServiceApp.MyServiceService.Service1>, MyServiceApp.MyServiceService.Service1 {
my testclass:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace MyServiceApp.UnitTests
{
[TestClass]
public class ShowroomLogViewModelTests
{
[TestMethod]
public void ReloadShowroomLogs_IsBusyShouldBeFalse()
{
var mockedWebService = Mock<IService1>;
Problem is that:
IService1 cannot be resolved
How can I mock my wcf service?
EDIT: One Step Closer
var mockedWebService =new Mock<MyService.Service1>;
I've changed to this and now is compiling, but...
I haven' access to method, e.g.
mockedWebService.GetShowroomLogs();
It is a big problem, because, I need change a property ServiceClient at ShowroomLogViewModel to interface, and I can't because then my viewmodel can't use wcf service methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该这样做:mockedWebService.Object.GetShowroomLogs();
You should do this: mockedWebService.Object.GetShowroomLogs();