Faking Files, Data, Git interractions with mocks/stubs/fixtures

I’m new to ruby and just learning cucumber and rspec. I have some
examples(tests) that need to fake a directory structure and file
grouping, fake a properly formatted json file and fake a ruby-git
interraction (git.fetch, git.merge).

I’ve been reading about mocks and stubs but still feeling a little in
the dark.

Given some example tests like the following

describe “#update_repo” do
it “should update the repository without errors” do
git = double(‘git’)
# How do I mock this so that the Git object is “faked” and the
calls to fetch/merge don’t cause an error without actually talking to a
remote repo
lambda { @app.update_repo git }.should_not raise_error
end
end

describe “#check_json” do
it “should check that the har files are valid json if they exist” do
# In the real context @app.files contains a list of har json
files
# How do I mock the json files on the file system
test_contents = @app.organize_test_contents @app.files
# Can I mock test_contents or use a fixture?
result = @app.check_json test_contents
result.should =~ /JSON Valid/
end
end

What are some recommended ways for faking data, faking file systems,
faking network interractions

On 19 Apr 2011, at 19:24, Josh N. wrote:

describe “#update_repo” do
it “should check that the har files are valid json if they exist” do
What are some recommended ways for faking data, faking file systems,
faking network interractions

Quick answer:

One well-trodden path is to use the adapter pattern to isolate the
behaviour that actually interacts with the external file / network
system behind an interface. I’d recommend Steve Freeman and Nat Pryce’s
book “Growing Object Oriented Software, Guided by Tests” (GOOS) where
they talk about this in detail, as well as many other very useful
testing techniques. The examples are in java, but the design principles
apply equally to Ruby code.


Posted via http://www.ruby-forum.com/.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

[email protected]
07974 430184

I’ve been using the VCR gem lately and it is great.
Maybe this would be useful if you’re fetching from git using http??

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs