Skip to content

Instantly share code, notes, and snippets.

View ifew's full-sized avatar
😙
wu hooo!

Chitpong Wuttanan (iFew) ifew

😙
wu hooo!
View GitHub Profile
@ifew
ifew / IAuthorizeMockTestMoq4.cs
Last active July 8, 2018 16:15
.Net Core Mock Test with Moq4
public void When_Member_Access_to_Profile_By_Using_Moq() {
var mock = new Mock<IAuthorize>();
mock.Setup(auth => auth.CheckAuthorize(It.IsAny<string>(), It.IsAny<string>())).Returns(true).Verifiable();
Member member = new Member(mock.Object);
mock.Verify(auth => auth.CheckAuthorize(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
@ifew
ifew / IAuthorizeDummyTest.cs
Last active July 8, 2018 14:52
.Net Core 2 Dummy (Unit Test)
using System;
using Xunit;
namespace member_dummy
{
public class MemberTest
{
[Fact]
public void When_Called_Member_Should_Be_Object_Type_Member() {
Member member = new Member(new DummyAuthorize());
@ifew
ifew / FakeUnitTest.cs
Created July 5, 2018 16:48
.Net Core 2 Fake
public void Display_Username_Shold_Be_iFew()
{
var _options = new DbContextOptionsBuilder<MemberContext>().UseInMemoryDatabase("get_by_id_members").Options;
var _context = new MemberContext(_options);
_context.Members.Add(
new MemberModel
{
Id = 1,
@ifew
ifew / IAuthorizeMock.cs
Created July 5, 2018 15:47
.Net Core 2 Mock
public class MockAuthorize : IAuthorize {
public Boolean checkAuthorizeWasCalled = false;
public Boolean CheckAuthorize(string username, string password) {
checkAuthorizeWasCalled = true;
return true;
}
public Boolean VerifyCheckAuthorize(){
return checkAuthorizeWasCalled;
@ifew
ifew / IAuthorizeMockTest.cs
Last active July 5, 2018 15:44
.Net Core Mock Test
public void When_Member_Access_to_Profile() {
MockAuthorize mock = new MockAuthorize();
Member member = new Member(mock);
string is_authorized = member.Profile();
Assert.True(mock.VerifyCheckAuthorize());
Assert.Equal("Welcome to member area", is_authorized);
}
@ifew
ifew / IAuthorizeSpy.cs
Created July 5, 2018 09:57
.Net Core Spy
public class SpyAuthorize : IAuthorize {
public int checkAuthorizeWasCalled = 0;
public Boolean CheckAuthorize(string username, string password) {
checkAuthorizeWasCalled++;
return true;
}
}
@ifew
ifew / IAuthorizeSpyTest.cs
Created July 5, 2018 09:48
.Net Core 2 Spy Test
using System;
using Xunit;
namespace member_spy
{
public class MemberTest
{
[Fact]
public void When_Member_Access_to_Profile() {
SpyAuthorize spy = new SpyAuthorize();
@ifew
ifew / IAuthorizeStub.cs
Created July 5, 2018 09:17
.Net Core 2 Stub
public class StubAuthorize : IAuthorize {
public Boolean CheckAuthorize(string username, string password) {
return true;
}
}
}
@ifew
ifew / IAuthorizeStubTest.cs
Created July 5, 2018 09:13
.Net Core 2 Stub Test
using System;
using Xunit;
namespace member_stub
{
public class MemberTest
{
[Fact]
public void When_Member_Access_to_Profile() {
Member member = new Member(new StubAuthorize());
@ifew
ifew / Member.cs
Created July 5, 2018 08:32
.Net Core 2 Test Doubles
using System;
namespace member_dummy
{
public class Member
{
Boolean authorized;
public Member(IAuthorize authorize) {
this.authorized = authorize.CheckAuthorize("ifew", "1234");