Home Community Blog Buy Now
Blog

Misty Community Forum

How do I mock _misty in C#?

I am trying to create a unit test for a C# class library but I need to mock _misty because I am passing it to the class library - see sample code below.

Previously MistyRobotics.SDK contained IMockRobot. However it looks like it has been removed from the SDK.


 public class SimulatedMisty
{
      public IMockRobot MockedRobot { get; }

I want to mock the logging with Misty so it just does not generate an error.:

                _misty.SkillLogger.LogVerbose($"Misty.Services: IN GetJokeInfo() -- MistyApiInfo.JokeBaseUrl: {MistyApiInfo.JokeBaseUrl}");

How do I mock the SkillLogger so it does not generate an error?

Thanks for any help.

Wes


=============================


        [TestMethod]
        public void TestMistyJokeApi()
        {
            MistyJokeApi mistyJoke = new MistyJokeApi(_misty);
            string jokeText = mistyJoke.GetJokeInfo();
            Assert.IsTrue(true);
        }

=============================

using System;
using System.Net.Http;
using MistyRobotics.SDK.Messengers;
using Newtonsoft.Json;

namespace Misty.Services
{
    /// <summary>
    /// This service calls a Joke API to return a random joke to Misty.
    /// </summary>
    public class MistyJokeApi
    {
        private IRobotMessenger _misty;

        public MistyJokeApi(IRobotMessenger misty1)
        {
            _misty = misty1;
        }

        // API Website:  https://icanhazdadjoke.com/
        public string GetJokeInfo()
        {
            string jokeText = "";
            try
            {
                _misty.SkillLogger.LogVerbose($"Misty.Services: IN GetJokeInfo() -- MistyApiInfo.JokeBaseUrl: {MistyApiInfo.JokeBaseUrl}");
                var response = GetResponse(MistyApiInfo.JokeBaseUrl);
                Newtonsoft.Json.Linq.JObject jsonResp = Newtonsoft.Json.Linq.JObject.Parse(response);
                _misty.SkillLogger.LogVerbose($"Misty.Services: IN GetJokeInfo() -- jsonResp: {jsonResp.ToString(Formatting.Indented)}");
                jokeText = (string)jsonResp["joke"];
                _misty.SkillLogger.LogVerbose($"Misty.Services: IN GetJokeInfo() -- jokeText: {jokeText}");
            }
            catch (Exception ex)
            {
                _misty.SkillLogger.Log($"Misty.Services: IN GetJokeInfo() => Exception", ex);
            }
            return jokeText;
        }

        private string GetResponse(string url)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                return httpClient.GetStringAsync(new Uri(url)).Result;
            }
        }
    }
}

Hey Wes,
You can use Moqs for unit testing of the skill. There is a simple example of a skill with basic unit tests here: https://github.com/MistyCommunity/.NET-SDK/tree/master/C%23/Sample%20Projects/UnitTestExampleSkill

Let us know if that gets you what you need.
Thanks!

Hey Brad,

That is exactly what I was looking for. I will try using it tonight.

Much appreciated - thanks.

Wes