Created
September 17, 2012 04:43
-
-
Save Gab-km/3735610 to your computer and use it in GitHub Desktop.
「Rhino.Mocksを使ってみた」のサンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Rhino.Mocks.Sample | |
{ | |
public interface IAlgorithm | |
{ | |
string Run(int nuber); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
using Rhino.Mocks.Sample; | |
namespace Rhino.Mocks.Test | |
{ | |
public class IAlgorithmTest | |
{ | |
[Test] | |
public void Mockの確認() | |
{ | |
var mock = MockRepository.GenerateMock<IAlgorithm>(); | |
mock.Expect(m => m.Run(1)).Return("1"); | |
mock.Expect(m => m.Run(2)).Return("2"); | |
mock.Expect(m => m.Run(3)).Return("Fizz"); | |
Assert.That(mock.Run(1), Is.EqualTo("1")); | |
Assert.That(mock.Run(2), Is.EqualTo("2")); | |
Assert.That(mock.Run(3), Is.EqualTo("Fizz")); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace Rhino.Mocks.Sample | |
{ | |
public class ListBuilder | |
{ | |
private IAlgorithm algorithm; | |
public ListBuilder(IAlgorithm algorithm) | |
{ | |
this.algorithm = algorithm; | |
} | |
public List<string> Build(int start, int count) | |
{ | |
return Enumerable.Range(start, count).Select(this.algorithm.Run).ToList(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using NUnit.Framework; | |
using Rhino.Mocks.Sample; | |
namespace Rhino.Mocks.Test | |
{ | |
public class ListBuilderTest | |
{ | |
[Test] | |
public void ListBuilderにいろんなAlgorithmを渡してみる() | |
{ | |
var fizzbuzz = MockRepository.GenerateMock<IAlgorithm>(); | |
fizzbuzz.Expect(m => m.Run(1)).Return("1"); | |
fizzbuzz.Expect(m => m.Run(2)).Return("2"); | |
fizzbuzz.Expect(m => m.Run(3)).Return("Fizz"); | |
fizzbuzz.Expect(m => m.Run(4)).Return("4"); | |
fizzbuzz.Expect(m => m.Run(5)).Return("Buzz"); | |
var fizzbuzzBuilder = new ListBuilder(fizzbuzz); | |
Assert.That(fizzbuzzBuilder.Build(1, 5), | |
Is.EqualTo(new List<string>() { "1", "2", "Fizz", "4", "Buzz" })); | |
var fibbuzz = MockRepository.GenerateMock<IAlgorithm>(); | |
fibbuzz.Expect(m => m.Run(1)).Return("1"); | |
fibbuzz.Expect(m => m.Run(2)).Return("1"); | |
fibbuzz.Expect(m => m.Run(3)).Return("2"); | |
fibbuzz.Expect(m => m.Run(4)).Return("Fizz"); | |
fibbuzz.Expect(m => m.Run(5)).Return("Buzz"); | |
var fibbuzzBuilder = new ListBuilder(fibbuzz); | |
Assert.That(fibbuzzBuilder.Build(1, 5), | |
Is.EqualTo(new List<string>() { "1", "1", "2", "Fizz", "Buzz" })); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment