Skip to content

Instantly share code, notes, and snippets.

@karl-sjogren
Last active August 10, 2023 12:40
Show Gist options
  • Save karl-sjogren/af0f6db69f4c0e473f66241ec10e8eff to your computer and use it in GitHub Desktop.
Save karl-sjogren/af0f6db69f4c0e473f66241ec10e8eff to your computer and use it in GitHub Desktop.

Regexes to replace Moq with FakeItEasy

Replace Mock with Fake

(new\sMock|Mock\.of)<
A.Fake<

Mock<([a-z]*?)(?:\s*?)>
$1

Replace simple return setups

([a-z]+?)(?:[\s\n]*?).Setup(?:Get)?\((?:[a-z]*?)\s=>\s(?:[a-z\s\n]*?)\.([a-z0-9._="'<>,\s\n[\]()]*)\)([\s\n]*?).Returns(?:Async)?(<?[a-z0-9]*?>?)\(((?:.|[\n])*?)\);
A.CallTo(() => $1.$2)$3.Returns$4($5);

Replace MyMock.Object with just MyMock

([a-z]*?)\.Object([,);\s])
$1$2

Replace setup with throws

([a-z]*?)(?:[\s\n]*?).Setup\((?:[a-z]*?) => (?:[a-z]*?)\.([a-z0-9.="'_<>,\s[\]()]*)\)([\s\n]*?).Throws(Async)?\(((?:.|[\n])*?)\);
A.CallTo(() => $1.$2)$3.Throws$4($5);

Replace setup with callbacks

([a-z]+?)(?:[\s\n]*?).Setup(?:Get)?\((?:[a-z]*?)\s=>\s(?:[a-z\s\n]*?)\.([a-z0-9._="'<>,\s\n[\]()]*)\)([\s\n]*?).Callback(?:Async)?(<?[a-z0-9,\s]*?>?)\(((?:.|[\n])*?)\);
A.CallTo(() => $1.$2)$3.Invokes($5);

Replace It.IsAny() with A<>._

It\.IsAny<([a-z<>[\]]*?)>\(\)
A<$1>._

Replace It.Is with A<>.That.Matches

It\.Is<([a-z<>[\]]*?)>\((.*?)\)
A<$1>.That.Matches($2)

Fix lazy returns

\.Returns(<?[a-z0-9]*?>?)\((\(.*?\)|[a-z0-9]*?)\s=>
.ReturnsLazily$1($2 =>

Times and stuff

([a-z]*?)(?:\s*?).Verify\((?:[a-z]*?)\s=>\s(?:[a-z\s\n]*?)\.([a-z0-9._="'<>,\s[\]()]*),[\s\n]*?Times.Once\((.*?)\)\);
A.CallTo(() => $1.$2)$3.MustHaveHappened();

([a-z]*?)(?:\s*?).Verify\((?:[a-z]*?)\s=>\s(?:[a-z\s\n]*?)\.([a-z0-9._="'<>,\s[\]()]*),[\s\n]*?Times.Never\((.*?)\)\);
A.CallTo(() => $1.$2)$3.MustNotHaveHappened();

([a-z]*?)(?:\s*?).Verify\((?:[a-z]*?)\s=>\s(?:[a-z\s\n]*?)\.([a-z0-9._="'<>,\s[\]()]*),[\s\n]*?Times.Exactly\((.*?)\)\);
A.CallTo(() => $1.$2).MustHaveHappened($3, Times.Exactly);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment