Skip to content

Instantly share code, notes, and snippets.

@jhgbrt
jhgbrt / GZipCompressDecompress.cs
Last active February 22, 2023 16:02
C# gzipstream
public static byte Compress(string text)
{
using (var ms = new MemoryStream())
{
using (var zip = new GZipStream(ms,
CompressionMode.Compress))
using (var writer = new StreamWriter(zip, Encoding.UTF8))
{
writer.Write(text);
}
[Test]
public void EnsureDependencyIsCalledWithCorrectArguments()
{
var dependency = MockRepository.GenerateMock<IDependency>();
dependency.Expect(x => x.DoSomething(null, 0))
.IgnoreArguments().Return(7).Repeat.Once();
dependency.Expect(x => x.DoSomething(null, 0))
.IgnoreArguments().Return(3).Repeat.Once();
var systemUnderTest = new SystemUnderTest(dependency);
@jhgbrt
jhgbrt / moq_verifications.cs
Created May 30, 2012 19:23
Verifying method calls with Moq
[Test]
public void UsingMoqWithVerifications()
{
// Arrange
var dependency = new Mock<IDependency>();
// unfortunately there does not seem to be a simple way to set multiple return values with Moq...
var returnValues = new[] {7, 3};
var index = -1;
dependency.Setup(x => x.DoSomething(It.IsAny<SomeComplexType>(), It.IsAny<int>()))
@jhgbrt
jhgbrt / ProcessHelper.cs
Last active September 30, 2015 15:18
C#/.Net class for launching a CommandLine process and capturing stdout/stderr
public class ProcessHelper
{
/// <summary>
/// Starts a command line process, redirecting both StdOut and StdErr.
/// </summary>
/// <param name="fileName">Path to the executable</param>
/// <param name="arguments">Command line arguments</param>
/// <param name="onErr">Action to perform when data from the child process is read from StdErr. The action takes 2 parameters: a Process instance (representing the child process) and a string (the data it wrote to StdErr)</param>
/// <param name="onOut">Action to perform when data from the child process is read from StdOut. The action takes 2 parameters: a Process instance (representing the child process) and a string (the data it wrote to StdOut)</param>
/// <returns></returns>