我的项目是一个机器人电报。我有一个名为Mainclass
的类。我想测试它的方法(SubmitNewsGetType
)。这个方法没有输入和输出。这个方法检查收到的消息来自用户并发送适当的回复。这是我对班级和方法的捍卫。
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");
}
}
这是我的单元测试。我的问题是我无法初始化消息类型。
[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 ....
}
答案 0 :(得分:1)
TelegramBot
功能的界面,并让TelegramBot
实现它。 interface ITelegramBot
{
Message SendMessage(MessageTarget target, string msg);
}
bot
成员的类型更改为ITelegramBot
public ITelegramBot bot;
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;
}
}
[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);
}
答案 1 :(得分:0)
尝试将@NSManaged var weeks: Set<Week>
更改为Message mess_input="/submitnews"
您还错过了收到错误的一行的半冒号。
这是一个解释更多的链接: https://msdn.microsoft.com/en-us/library/system.messaging.message(v=vs.110).aspx