Josh N.
2011-Apr-19 18:24 UTC
[rspec-users] 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 -- Posted via http://www.ruby-forum.com/.
Matt Wynne
2011-Apr-21 09:08 UTC
[rspec-users] Faking Files, Data, Git interractions with mocks/stubs/fixtures
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 interractionsQuick 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 at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-userscheers, Matt matt at mattwynne.net 07974 430184
andyl
2011-Apr-21 23:29 UTC
[rspec-users] Faking Files, Data, Git interractions with mocks/stubs/fixtures
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?? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110421/eff8938d/attachment.html>