On 19 Apr 2011, at 19:24, Josh N. wrote: > 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
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 > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users cheers, Matt m...@mattwynne.net 07974 430184 _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users