Faking a git repository for testing
I am writing an application that wraps git
calls. I need to be able to test it (using rspec
), so I'm archiving a git repository and extracting it upon test execution.
However, this is becoming tedious because making a change to the test repository means repackaging everything. Also, I need to be able to remember exactly what is contained in the git repository, which makes it hard to use the test repository for multiple test files.
I am looking for something like fakefs where maybe I could do something like the following:
before :all do
@test_repo = FakeGit::init
@test_repo.add(file)
@test_repo.commit(file, "Some message") ===> returns "SHA_ID"
end
The开发者_高级运维n somewhere in my test code, I could do something like:
it "should point to a SHA_ID" do
@test_repo.rev_parse("HEAD").should == "SHA_ID"
end
Does your application do anything beyond wrapping git calls? Does it employ any logic beyond invoking git that need to be tested? If it's simply a wrapper, then testing your application will amount to testing git itself, which is probably redundant.
But if your application brings to the table things beyond simple git calls, you're better off creating a "git call" interface (or the closest ruby equivalent) that you mock in your tests. You would then be testing your application logic, all the way to when it has to touch actual git calls.
Look here for a concise account of the distinction between mocks and fakes. You seem to be trying to create a fake, or even use the real thing. I would recommend looking into mocking rather than faking/stubbing.
精彩评论