如何通过Moq模拟wcf服务(由asmx转换)

发布于 2024-12-07 13:34:01 字数 1759 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-12-14 13:34:01

你应该这样做:mockedWebService.Object.GetShowroomLogs();

You should do this: mockedWebService.Object.GetShowroomLogs();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文