我正在写一个应用程序,当按下窗口中的按钮LoginWindow Loger通过数据库chcecks如果传递的凭据是好的。如果它返回一些东西(非null)我想检查loger.validateuser()是否引发了LoginSucces事件。如何测试?
namespace Komora.Classes.Loger
{
public abstract class Loger
{
public delegate void LoginStatus(object sender, EventArgs e);
public event LoginStatus loginSucces;
public event LoginStatus loginFailed;
public abstract void validateUser(string login, string password) ;
protected void RaiseLoginSuccesEvent(EventArgs eventArgs)
{
if (loginSucces != null)
{
loginSucces(this, eventArgs);
}
}
protected void RaiseLoginFailedEvent(EventArgs eventArgs)
{
if (loginFailed != null)
{
loginFailed(this, eventArgs);
}
}
}
}
namespace Komora.Classes.Loger
{
public class LogerDataBase : Loger
{
#region Private Variables
DataBase.IDataBaseConncection dataBaseConnection;
#endregion
#region Constructor
public LogerDataBase(DataBase.IDataBaseConncection dataBaseConnection)
{
this.dataBaseConnection = dataBaseConnection;
}
#endregion
#region Derived Methods
public override void validateUser(string login, string password)
{
if (LogInSuccess(login, password))
{
RaiseLoginSuccesEvent(EventArgs.Empty);
}
else
{
RaiseLoginFailedEvent(EventArgs.Empty);
}
}
#endregion
#region Private Methods
private bool LogInSuccess(string login, string password)
{
if (null != dataBaseConnection.getUserByCredentials(login, password))
return true;
else
return false;
}
#endregion
}
}
public class LogerDataBaseTests
{
[TestCase]
public void ValidateUserCalledWithGoodUserCredentialsRaisesLoginSuccesEventShouldPass()
{
var dataBaseConnectionMock = new Mock<Classes.DataBase.IDataBaseConncection>();
dataBaseConnectionMock.Setup(m => m.getUserByCredentials(It.IsAny<string>(), It.IsAny<string>())).Returns(new object());
var loger = new Classes.Loger.LogerDataBase(dataBaseConnectionMock.Object);
loger.validateUser("user", "pass");
}
}
答案 0 :(得分:4)
只需将您的测试订阅到该事件并检查它是否被引发:
public class LogerDataBaseTests
{
[TestCase]
public void ValidateUserCalledWithGoodUserCredentialsRaisesLoginSuccesEventShouldPass()
{
var dataBaseConnectionMock = new Mock<Classes.DataBase.IDataBaseConncection>();
dataBaseConnectionMock.Setup(m => m.getUserByCredentials(It.IsAny<string>(), It.IsAny<string>())).Returns(new object());
var loger = new Classes.Loger.LogerDataBase(dataBaseConnectionMock.Object);
bool successRaised = false;
bool failureRaised = false;
loginSucces += (s, e) => {successRaised = true;}
loginFailed += (s, e) => {failureRaised = true;}
loger.validateUser("user", "pass");
Assert.IsTrue(successRaised);
Assert.IsFalse(failureRaised);
}
}
答案 1 :(得分:0)
进行两次测试,一次是“成功”,一次是“失败”。
[TestClass]
public class LogerDataTest
{
[TestMethod]
public void validateUser_getUserByCredentialsReturnsNotNull_loginSuccesEventIsRaised()
{
// Arrange
const int loginSuccesExpected = 1;
int loginSuccesActual = 0;
Mock<DataBase.IDataBaseConncection> dataConnectionStub = new Mock<DataBase.IDataBaseConncection>();
dataConnectionStub.Setup(db => db.getUserByCredentials("loginFake", "passwordFake")).Returns("User");
LogerDataBase logerData = new LogerDataBase(dataConnectionStub.Object);
logerData.loginSucces += (sender, args) =>
{
loginSuccesActual++;
};
// Act
logerData.validateUser("loginFake", "passwordFake");
// Assert
Assert.AreEqual(loginSuccesExpected, loginSuccesActual);
}
[TestMethod]
public void validateUser_getUserByCredentialsReturnsNull_loginFailedEventIsRaised()
{
// Arrange
const int loginFailedExpected = 1;
int loginFailedActual = 0;
Mock<DataBase.IDataBaseConncection> dataConnectionStub = new Mock<DataBase.IDataBaseConncection>();
dataConnectionStub.Setup(db => db.getUserByCredentials("loginFake", "passwordFake")).Returns(null);
LogerDataBase logerData = new LogerDataBase(dataConnectionStub.Object);
logerData.loginFailed += (sender, args) =>
{
loginFailedActual++;
};
// Act
logerData.validateUser("loginFake", "passwordFake");
// Assert
Assert.AreEqual(loginFailedExpected, loginFailedActual);
}
}