Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class.
C#
public class VoiceElementsLine : ILine
   {
       public Guid LineId { get; set; }
       public TelephonyServer TelephonyServer { get; set; }
       public virtual ChannelResource ChannelResource { get; set; }
       public LanguageData LangInfo { get; set; }

       public string InboundDnis
       {
           get { return _inboundDnis; }
       }

       private readonly Logger _appLog = GetCurrentLogger();
       private SipChannel _incomingSipChannel;
       public VoiceResource IncomingVoiceResource { get; private set; }
       private ServerInfo _serverInfo;
       private string _inboundDnis, _inboundAni, _inboundIp;
       private DateTime _callDate;

       public void RunCallScript()
       {
           SetupLine();
           AnswerSipChannel();
           var callFlowApp = GetCallFlowApplication();
           callFlowApp.HandleCall();
           Hangup();
       }

       private void AnswerSipChannel()
       {
           _incomingSipChannel.Answer();
       }

       public ICallFlowApplication GetCallFlowApplication()
       {
           var inboundValues = _inboundDnis.Split('_');
           var appId = byte.Parse(inboundValues[0]);

           var appFactory = GetApplicationFactoryInstance();
           var callFlowApp = appFactory.GetApplication(appId);
           return callFlowApp;
       }

       public IApplicationFactory GetApplicationFactoryInstance()
       {
           return VoiceElementsCallFlowService.Container.GetInstance<IApplicationFactory>();
       }

       public void PlayPromptTextToSpeech(string promptText)
       {
           IncomingVoiceResource.PlayTTS(promptText);
       }

       public void PlayPromptFile(string promptFile)
       {
           IncomingVoiceResource.Play(promptFile);
       }

       public string GetUserInput(int maxDigits)
       {
           IncomingVoiceResource.MaximumDigits = maxDigits;
           return IncomingVoiceResource.DigitBuffer;
       }

       private void in_ChannelResource_Disconnected(object sender, DisconnectedEventArgs e)
       {
           _appLog.Info("Inbound Disconnect Event");
       }

       public void SetupLine()
       {
           try
           {
               _incomingSipChannel = ChannelResource as SipChannel;
               _incomingSipChannel.Disconnected += in_ChannelResource_Disconnected;
               _serverInfo = TelephonyServer.GetServerInfo();
               IncomingVoiceResource = _incomingSipChannel.VoiceResource;
               IncomingVoiceResource.Codec = Codec.PCM_8Khz_8Bit;

               _inboundDnis = ChannelResource.Dnis;
               // Ex: if dialstring is facilityidInmatePIN@10.50.96.222
               // then InboundDnis = languagefacilityIdInmatePIN; 1_1_163_1234@10.50.96.222
               _inboundAni = ChannelResource.Ani;
               _inboundIp = _incomingSipChannel.RemoteCallControlAddress;
               _callDate = DateTime.Now;

               LogSetupLineInformation();
           }
           catch (Exception ee)
           {
               _appLog.Error(ee);
           }
       }

The corresponding interface ILine
C#
public interface ILine
   {
       Guid LineId { get; set; }

       void RunCallScript();
       void PlayPromptTextToSpeech(string promptText);
       void PlayPromptFile(string promptFile);
       string GetUserInput(int maxDigits);
       void Hangup();
   }

In my unit tests, first I want to make sure the method SetupLine is executed. So I use Moq.
C#
[Fact]
        public void SetupLine_Should_Properly_Setup_Codec()
        {
            // ARRANGE
            Mock<VoiceElementsLine> voiceElementsLineMock = new Mock<VoiceElementsLine>()
            { CallBase = true};
            Mock<TelephonyServer> telephonyServerMock = new Mock<TelephonyServer>();
            Mock<ChannelResource> channelResourceMock = new Mock<ChannelResource>(telephonyServerMock) ;
            voiceElementsLineMock.SetupGet(x => x.ChannelResource).Returns(channelResourceMock.Object);
            var voiceElementsLine = voiceElementsLineMock.Object;

            // ACT
            voiceElementsLine.SetupLine();

            // ASSERT
            Assert.True(voiceElementsLine.IncomingVoiceResource.Codec== Codec.PCM_8Khz_8Bit);
        }

Please notice notice ChannelResource has a constructor which is TelephonyServer.
However I got the error
C#
An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code


Thanks for help with the code and hints.
Posted
Updated 10-Jan-23 6:03am
v3
Comments
ZurdoDev 15-Oct-15 9:08am    
The error tells you what to do. What exactly is your question?

In your test, you are mocking the concrete class 'VoiceElementsLine'. You should be mocking interfaces - not concrete classes (though you are able to do so in Moq if the concrete class has a public default constructor).

So, in your test you should use:
C#
Mock<ILine> voiceElementsLineMock = new Mock<ILine>() { CallBase = true};
 
Share this answer
 
I supose your class
C#
ChannelResource
dont have a default constructor or empty,
C#
public void ChannelResource(){}
 
Share this answer
 
Comments
[no name] 15-Oct-15 9:31am    
But it has a constructor. Even I imagine it doesn't have a default constructor, the error is still there.
you need to implement all mehtos of teh interface, public void Hangup(){} is not implement yet
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900