Skip to content

Instantly share code, notes, and snippets.

package crypto_test
import (
"go_web_server/pkg/crypto"
"testing"
)
func Test_Hash(t *testing.T) {
t.Run("Can hash and compare", should_be_able_to_hash_and_compare_strings)
t.Run("Can detect unequal hashes", should_return_error_when_comparing_unequal_hashes)
@eamonnmcevoy
eamonnmcevoy / hash.go
Created April 15, 2017 05:51
crypto/hash.go
package crypto
import (
"errors"
"strings"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
@eamonnmcevoy
eamonnmcevoy / hash.go
Created April 15, 2017 05:49
root.Hash interface
package root
type Hash interface {
Generate(s string) (string, error)
Compare(hash string, s string) error
}
var mockUserService = new MockUserService();
var mockResultService = new MockResultService();
var testTime = new DateTime(1,1,2000);
var resultGenerator = new ResultGenerator(mockUserService, mockResultService, testTime);
public class TesableResultGenerator : ResultGenerator
{
private IUserService _userService;
private IResultService _resultService;
private DateTime _dateTime;
public TestableResultGenerator(IUserService userService, IResultService resultService, DateTime dateTime)
: base() {
_userService = userService;
_resultService = resultService;
using System.Text;
using System;
namespace BigLegacyApplication
{
internal class ReportGenerator
{
public ReportGenerator(){}
internal string GetUserReport(int userId)
var mockUserService = new MockUserService();
var mockResultService = new MockResultService();
var resultGenerator = new ResultGenerator();
resultGenerator.userService = mockUserService;
resultGenerator.mockResultService = mockResultService;
private readonly IUserService _userService;
public IUserService userService {
get {
if(_userService == null)
_userService = new UserService();
return _userService;
}
set { _userService = value; }
}
internal interface IUserService
{
User GetUser(int userId);
}
internal interface IResultService
{
string[] GetUserResults(int userId);
}
@eamonnmcevoy
eamonnmcevoy / ReportGenerator.cs
Created March 29, 2017 07:33
Example of un-testable class
using System.Text;
using System;
namespace BigLegacyApplication
{
internal class ReportGenerator
{
public ReportGenerator()
{
}