Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created December 11, 2014 11:06
Show Gist options
  • Save hatelove/3122ef16b4ab999af016 to your computer and use it in GitHub Desktop.
Save hatelove/3122ef16b4ab999af016 to your computer and use it in GitHub Desktop.
from Rhino.Mocks to NSub
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//using Rhino.Mocks;
using RsaSecureToken;
using NSubstitute;
namespace RsaSecureToken.Tests
{
[TestClass()]
public class AuthenticationServiceTests
{
[TestMethod()]
public void IsValidTest_只有驗證Authentication合法或非法()
{
//arrange
//IProfile profile = MockRepository.GenerateStub<IProfile>();
//profile.Stub(x => x.GetPassword("Joey")).Return("91");
IProfile profile = Substitute.For<IProfile>();
profile.GetPassword("Joey").Returns("91");
//IToken token = MockRepository.GenerateStub<IToken>();
//token.Stub(x => x.GetRandom("Joey")).Return("abc");
IToken token = Substitute.For<IToken>();
token.GetRandom("Joey").Returns("abc");
//ILog log = MockRepository.GenerateStub<ILog>();
ILog log = Substitute.For<ILog>();
AuthenticationService target = new AuthenticationService(profile, token, log);
string account = "Joey";
string password = "wrong password";
// 正確的 password 應為 "91abc"
//act
bool actual;
actual = target.IsValid(account, password);
// assert
Assert.IsFalse(actual);
}
[TestMethod]
public void IsValidTest_如何驗證當非法登入時有正確紀錄log()
{
//arrange
//IProfile profile = MockRepository.GenerateStub<IProfile>();
//profile.Stub(x => x.GetPassword("Joey")).Return("91");
IProfile profile = Substitute.For<IProfile>();
profile.GetPassword("Joey").Returns("91");
//IToken token = MockRepository.GenerateStub<IToken>();
//token.Stub(x => x.GetRandom("Joey")).Return("abc");
IToken token = Substitute.For<IToken>();
token.GetRandom("Joey").Returns("abc");
ILog log = Substitute.For<ILog>();
//// step 1: 建立 mock object
//var mock = new MockRepository();
//ILog log = mock.StrictMock<ILog>();
var target = new AuthenticationService(profile, token, log);
var account = "Joey";
var password = "wrong password";
//Act
target.IsValid(account, password);
//Assert
log.Received().Save("account:Joey try to login failed");
//// step 2: 建立預期腳本
//using (mock.Record())
//{
// var expected = "account:Joey try to login failed";
// log.Save(expected);
//}
//// step 3: 實際驗證
//using (mock.Playback())
//{
// var actual = target.IsValid(account, password);
//}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment