edit Correction:
My Error was that Instead of using ChatService.IChatService I was using ReflectorLike.ChatServiceReference.IchatService.
In other words , AFAIU I was referencing a reference to the Interface rather than the Interface it self. (If you find better way to explain that please suggest them I'll edit the answer later) If you run in the same error be sure that you use the correct Interface.
Issue:
The Error:
SetUp : System.InvalidOperationException : Cannot have two operations in the same contract with the same name, methods ClientConnectAsync and ClientConnect in type ReflectorLike.ChatReference.IChatService violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.
Summary: I tried to do this Recommended patterns for unit testing web services
But my mocked service throws me an exception because all my methods have twins with same name for instance it has both ClientConnect and ClientConnectAsync which violate WCF Service Rule
I have a service which interface is
namespace ChatService
{
[ServiceContract]
public interface IChatService
{
[OperationContract]
ChatUser ClientConnect(string userName);
[OperationContract]
void SendNewMessage(ChatMessage newMessage);
[OperationContract]
List<ChatUser> GetAllUsers();
[OperationContract]
void RemoveUser(ChatUser user);
[OperationContract]
List<ChatMessage> GetNewMessages(ChatUser user);
}
[DataContract]
public class ChatMessage
{
[DataMember]
public ChatUser User { get; set; }
[DataMember]
public string Message { get; set; }
private DateTime date;
[DataMember]
public DateTime Date
{
get { return date; }
set { date = value; }
}
}
/// <summary>
///
/// </summary>
[DataContract]
public class ChatUser
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public string IpAddress { get; set; }
[DataMember]
public string HostName { get; set; }
public ChatUser(string userName)
{
this.UserName = userName;
}
public override string ToString()
{
return this.UserName;
}
}
}
I want to test my client using a mock service so I Test it with nunit and nsubstitute
namespace ReflectorLike.Tests
{
[TestFixture]
internal class ChatHubTester
{
private ChatHub hub;
private ServiceHost host;
private IChatService myMockedService;
[SetUp]
public void SetUp()
{
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add<ServiceContractAttribute>();
myMockedService = Substitute.For<IChatService>();
host = MockServiceHostFactory.GenerateMockServiceHost(myMockedService, new Uri("http://localhost:12345"), "ServiceEndPoint");
host.Open();
hub=new ChatHub();
}
[TearDown]
public void TearDown()
{
host.Close();
}
[Test]
public void SomeTest()
{
hub.Connect("Test");
}
}
}