开发者

Entities storage for mocks

We would like to mock some services (usually, external) in our application. Mocks implemention should rely on data entities that should be easily editable in XML.

For example, mocking trivial method GetUsers:

class UserServiceMock : IUserService
{
    public List<User> GetUsers()
    {
        return MockRepository.Get<User>();
    }
}

And users list should be editable in Users.xml:

开发者_运维知识库<Users>
    <User id="1" name="TestUser" />
</Users>

Entities can be complex classes of course. It should be easy to add and configure new entities.

What's the best approach to implement such mocks?


For unit testing, I wouldn't rely on user-editable files to provide mock data. Instead, use Rhino.Mocks to mock out your IUserService and return data you need for unit testing.

var userService = MockRepository.GenerateStub<IUserService>();
userService.Stub(s => s.GetUsers()).Returns(new [] {
    new User { id = 1, name = "TestUser" }
    };

I usually use seed data (like "users.xml") for integration/stress testing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜