4

My project is a bot telegram.I have a class called Mainclass.I want to test a method of it(SubmitNewsGetType).This method has no input and output.This method check message received from user and send suitable response.This is my defenition of class and method.

public class MainClass
{
    enum state : int
    {
        _firstState = 0,
        _submitNewsGetType = 1,
        _submitNewsGetNews = 11,
        _reservationGym = 2, 
        _reservationGymGetSans = 22, 
        _pollingForFood = 3, 
        _appointment = 4, 
        _cancelingReservation = 5, 
        _cancelingAppointment = 6, 
        Registeration = 7 
    };

    public TelegramBot bot;
    private  int stateBot;
    public Message sendMessage;
    private  string tempString;
    public  Message message;
    private  MessageTarget target;

    public void SubmitNewsGetType()
    {
        if (message.Text.Contains("/submitnews"))
        {
           sendMessage = bot.SendMessage(target, "Please select a type news: \n/Elmi\n/Farhangi\n/Honari\n/Amoozeshi\n/Varzeshi\n\n/Return");
        }
        else if (message.Text.Contains("/Elmi") ||
                 message.Text.Contains("/Farhangi") ||
                 message.Text.Contains("/Honari") ||
                 message.Text.Contains("/Amoozeshi") ||
                 message.Text.Contains("/Varzeshi"))
        {
           sendMessage = bot.SendMessage(target, "Please enter your news: \n\n/Return");

            stateBot = (int)state._submitNewsGetNews;
            tempString = message.Text;
        }
        else
        {
            bot.SendMessage(target, "Non definded! Please select a type news: \n/Elmi\n/Farhangi\n/Honari\n/Amoozeshi\n/Varzeshi\n\n/Return");
        }
    }

And this is my unit test.My problem is I can't initialized Message type.

[TestMethod]
public void SubmitNewsGetNews_Test()
{
    MainClass instance = new MainClass();
    Message mess_output;

    //How to set value for a Message type??
    //This line get error
    Message mess_input="/submitnews"
    string expected="Please select a type news: \n/Elmi\n/Farhangi\n/Honari\n/Amoozeshi\n/Varzeshi\n\n/Return";

    if (mess_input is "/submitnews")
    {
        a.SubmitNewsGetType();
        mess_output= a.sendMessage;
        Assert.AreEqual(mess_output.Text,expected);
    } 
    else if (mess_input is "/elmi" or "/farhangi" or ...)
    else if ....
}
maryam
  • 1,437
  • 2
  • 18
  • 40
  • Possible duplicate of [How to test a class that has private methods, fields or inner classes?](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes) – Meirion Hughes Jan 30 '16 at 15:54

2 Answers2

1
  1. Create interface which cover TelegramBot functionality and make TelegramBot implements it.
interface ITelegramBot
{
    Message SendMessage(MessageTarget target, string msg);
}
  1. Change type of bot member to ITelegramBot
public ITelegramBot bot;
  1. Create "Fake" class of TelegramBot
class TelegramBotFake : ITelegramBot
{
    public Message ReturnMessage { get; set; }
    public string SendedMessage { get; set; }

    public Message SendMessage(MessageTarget target, string msg)
    {
        this.SendedMessage = msg;
        return this.ReturnMessage;
    }
}
  1. Test. Try to keep any logic operations away from the test methods. Any logic can contains bugs, bugs in the test method difficult to discover.
    Create own test for every possible result in your method - you will need more then one...
[TestMethod]
public void SubmitNewsGetNews_MessageContainsSubmitItems_SendPleaseSelectType()
{
    const string SUBMITITEMS = "/submitnews";
    TelegramBotFake fakebot = new TelegramBotFake();
    MainClass instance = new MainClass();
    instance.bot = fakebot; //Assign our fake bot
    instance.message.Text = SUBMITITEMS;
    string expected = "Please select a type news: \n/Elmi\n/Farhangi\n/Honari\n/Amoozeshi\n/Varzeshi\n\n/Return";

    instance.SubmitNewsGetType();

    //By checking faking bot.SendedMessage we sure that right method was executed
    Assert.AreEqual(instance.bot.SendedMessage, expected);
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
0

Try changing Message mess_input="/submitnews" to Message mess_input = new Message("/submitnews"); Also you were missing the semi colon at the line you were getting the error.

Here is a link that explains more: https://msdn.microsoft.com/en-us/library/system.messaging.message(v=vs.110).aspx

dev1998
  • 882
  • 7
  • 17
  • Message class comes from `telegrambotsharp`.This isn't a class in c#.
    https://core.telegram.org/bots/api#sendmessage
    – maryam Jan 30 '16 at 15:34