我刚开始使用 MSpec(James Broome 的 AutoMocking)和 RhinoMocks 使用 BDD/TDD。以下是我的实践项目的摘录:
namespace Tests.VideoStore.Controllers
{
public abstract class context_for_movie_controller :
Specification<MovieController>
{
private static IList<Movie> movies;
protected static IMovieRepository _movieRepository;
protected static ActionResult _result;
protected static string title;
protected static string director;
Establish context = () =>
{
_movieRepository = DependencyOf<IMovieRepository>();
};
}
[Subject(typeof(MovieController))]
public class when_searching_for_movies_with_director :
context_for_movie_controller
{
Establish context = () =>
{
title = null;
director = "James Cameron";
var movie4 = new Movie {
Title = "Terminator", Director = "James Cameron"};
var movie6 = new Movie {
Title = "Avatar", Director = "James Cameron"};
movies = new List<Movie> {movie4, movie6};
// Repository returns all movies.
_movieRepository.Stub(x => x.FindMovies(title, director))
.Return(movies);
};
Because of = () => _result = subject.Find(title, director);
It should_fetch_movies_from_the_repository = () =>
_movieRepository.AssertWasCalled(x =>
x.FindMovies(title, director));
It should_return_a_list_of_movies_matching_the_director = () =>
_result.ShouldBeAView().And()
.ShouldHaveModelOfType<IEnumerable<Movie>>)
.And().ShouldContainOnly(movies);
}
如您所见,我在 MovieRepository 类中删除了 FindMovies() 方法。然后我调用 MoviesController.Find() 操作。我的问题是,是否应该有一个断言来检查控制器是否调用了存根方法(FindMovies)?或者也许我应该只关心返回的结果而不关心它的来源?此外,“should_fetch_movies_from_the_repository”的规范看起来很像一项工程任务,而不是客户可能理解的东西——它在 BDD 中有它的位置吗?