From nick at ekenosen.net Fri Jun 1 00:06:09 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Fri, 1 Jun 2007 00:06:09 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> Message-ID: <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> On 5/31/07, David Chelimsky wrote: > On 5/31/07, nicholas a. evans wrote: > > So here's my latest thought-experiment: http://pastie.caboo.se/66478 > > And also, a simpler multi-field version: http://pastie.caboo.se/66510 > I took at a shot at something that feels more like the rest of RSpec. > It's admittedly more verbose than your examples, but I also think it > speaks well and aligns with everything else in rspec. WDYT? > > http://pastie.caboo.se/66679 Oh! I like it a *lot*. Yes, it's more verbose on each example than I prefer (8 characters plus two indentation levels plus the length of the block variable). But it does read more fluidly, and in many ways it looks much simpler. My "default_params" is simply your input to "given". My "columns" or "field_to_validate", is simply the argument(s) to "it_should_require_that". Likewise, "as" reads more naturally than :msg (although I had been thinking of using "with_message", which feels a bit more readable). I have only one real objection/concern: "given User.new" would pass in an object that would get reused for each accept/reject line in the block. I prefer the approach of creating a new/fresh object for each example. Perhaps it could accept a Proc or a symbol representing a method name, searching first for an instance method in the behavior, and then a class method on the class being described. "given :new" would get a different object from User.new before each example. "given User.new" would reuse the same new User object for all of the examples. "given :user_with_full_access" would call "user_with_full_access" (defined in that behavior or on User) before each example. "given user_with_full_access" would reuse the same object for all examples. What do you think? Is there a better way? A few more thoughts and questions: How about "it_should_validate_that" rather than "it_should_require_that"? That feels more explicit and to-the-point, to me. At that point, the ":behavior_type => :validation" becomes redundant. "it_should_validate_that" could simply be available to all model behaviors. What else could "given" be used for? It seems like this could become a pattern/approach that could extend beyond model validations... Have you thought about using it for anything else yet? How could we specify a *default* rejection message (or error count, etc)? Does "user.default_rejection_message = /the message/" (and so on) look reasonable? I did like that the each example/assertion got its own entry in the specdoc with my approach... Do you think that it is best to sum it up to the rule? Either way, I presume that a failure should still run all of examples to give a full report, rather than stop at the first wrong one. My approach assumed that if you were only working with one field, we should only check for errors on that one field (it's okay if the object is otherwise invalid). But if you are working with multiple fields, you should check to see if the entire object is valid, and check against all error messages. Do you think that is a safe and reasonable convention? Thanks for your thoughts... and your improvements on my thoughts. :-) -- Nick From daniel at danielfischer.com Fri Jun 1 00:09:21 2007 From: daniel at danielfischer.com (Fischer, Daniel) Date: Thu, 31 May 2007 21:09:21 -0700 Subject: [rspec-users] Could anyone please help with rspec/nested resource behavior checking? In-Reply-To: <7e565b5c0705310503l6af1f834n5d0363633787b59a@mail.gmail.com> References: <7e565b5c0705301703w334c3e2dg3f05d734ed76cd5a@mail.gmail.com> <1D95C70C-B551-43D3-AE07-5E7FFD9C5B74@parkerhill.com> <7e565b5c0705301807o42711e51w555ace5e8c037c84@mail.gmail.com> <7e565b5c0705302317s6a4ae3b7raa72f362a7f063d9@mail.gmail.com> <7e565b5c0705310131l441129dfif4441e2759d26afb@mail.gmail.com> <57c63afe0705310342h42ab6f51x77c3576c9f77f69c@mail.gmail.com> <7e565b5c0705310417u1d1fef0fv8a152b938c672966@mail.gmail.com> <57c63afe0705310428x57847426gaef8bc3d2733cfc1@mail.gmail.com> <7e565b5c0705310503l6af1f834n5d0363633787b59a@mail.gmail.com> Message-ID: <7e565b5c0705312109j107a524cmed721984b91538a2@mail.gmail.com> Alright so I removed the 'nested' resources, but I still have another problem. Most of my failures are gone, but I still have three left: 1) Spec::Mocks::MockExpectationError in 'PostsController handling GET /posts/1;edit should find the posts requested' Post expected :find with (any args) once, but received it twice ./spec/controllers/posts_controller_spec.rb:217: 2) Spec::Mocks::MockExpectationError in 'PostsController handling POST /posts should create a new posts' Mock 'Post_1027' received unexpected message :user_id= with (202018) /Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/posts_controller.rb:36:in `create' /Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in `process_without_test' ./spec/controllers/posts_controller_spec.rb:250:in `do_post' ./spec/controllers/posts_controller_spec.rb:256: 3) Spec::Mocks::MockExpectationError in 'PostsController handling POST /posts should redirect to the new posts' Mock 'Post_1028' received unexpected message :user_id= with (202018) /Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/posts_controller.rb:36:in `create' /Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in `process_without_test' ./spec/controllers/posts_controller_spec.rb:250:in `do_post' ./spec/controllers/posts_controller_spec.rb:260: Finished in 0.697209 seconds 38 examples, 3 failures I don't understand the first one, but I do have an idea on the second/third one. I am setting the @post.user_id to the current user in the controller, I'm trying to mock that behavior but whatever I try doesn't work. What would be the proper way? Right now I am doing this: it "should create a new posts" do Post.should_receive(:user_id).and_return(1) Post.should_receive(:new).with({'name' => 'Post'}).and_return(@post) do_post end still fails with that message though :( On 5/31/07, Fischer, Daniel wrote: > David, > > Awesome - well at least it's telling me something isn't working "that well" > anymore. > > Instead of the previous "expected success? to return true, got false" > > It's now "Couldn't find User with ID=1" > > But I swore that same code worked before, it looks proper in my pastie > doesn't it? > > *scratches head* - it certainly works when it's "live". > > On 5/31/07, David Chelimsky wrote: > > On 5/31/07, Fischer, Daniel wrote: > > > 1) > > > 'PostsController handling GET /users/1/posts should be successful' > FAILED > > > expected success? to return true, got false > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/expectations.rb:52:in > > > `fail_with' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/expectations/handler.rb:16:in > > > `handle_matcher' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/expectations/extensions/object.rb:32:in > > > `should' > > > ./spec/controllers/posts_controller_spec.rb:46: > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > 1.0.2/lib/spec/dsl/example.rb:73:in > > > `instance_eval' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/example.rb:73:in > > > `run_example' > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > 1.0.2/lib/spec/dsl/example.rb:23:in > > > `run' > > > /usr/local/lib/ruby/1.8/timeout.rb:48:in `timeout' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/example.rb:21:in > > > `run' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/behaviour.rb:72:in > > > `run' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/behaviour.rb:69:in > > > `each' > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > > > 1.0.2/lib/spec/dsl/behaviour.rb:69:in `run' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/runner/behaviour_runner.rb:45:in > > > `run_behaviours' > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > 1.0.2/lib/spec/runner/behaviour_runner.rb:44:in > > > `each' > > > > /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/runner/behaviour_runner.rb:44:in > > > `run_behaviours' > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > 1.0.2/lib/spec/runner/behaviour_runner.rb:27:in > > > `run' > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > > > 1.0.2/lib/spec/runner/command_line.rb:17:in `run' > > > /usr/local/lib/ruby/gems/1.8/gems/rspec- > 1.0.2/bin/spec:3: > > > /usr/local/bin/spec:18:in `load' > > > /usr/local/bin/spec:18: > > > > > > Doesn't really tell me anything, how about you? > > > > Nope. Thanks for playing ;) > > > > One thing - controller examples used to implicitly re-raise controller > > errors. We removed this in response to a bug report, which had the > > insidious side-effect of hiding errors without you knowing. If you > > upgrade to 1.0.4, you'll see a call in spec_helper.rb to > > raise_controller_errors. This is a new method that lets you do so > > explicitly, and is included in spec_helper.rb by default. > > > > I'm thinking this might help expose the problem. > > > > > > > > > > > On 5/31/07, David Chelimsky wrote: > > > > On 5/31/07, Fischer, Daniel wrote: > > > > > Oh my... > > > > > > > > > > I don't know what happened... but my entire posts_controller_spec > > > > > completely exploded and i'm having 31 failures, before I was having > > > > > like 8. I'm not sure what is going on, I could REALLY use the help. > > > > > > > > > > http://pastie.caboo.se/66440 for a list of everything that is going > > > > > on, and the code. > > > > > > > > Try running the one file with the -b switch to get a full backtrace. > > > > > > > > script/spec spec/controllers/posts_controller_spec.rb > -b > > > > > > > > > > > > > > I really appreciate the help guys, once I get over the mountain I'm > > > > > sure I'll understand it :) > > > > > > > > > > On 5/30/07, Fischer, Daniel < daniel at danielfischer.com> wrote: > > > > > > Okay that failed. > > > > > > > > > > > > I could really use a full blown example with this, just mocking > isn't > > > going > > > > > > to "give me insight" anymore I don't think. If anyone can post a > basic > > > crud > > > > > > behavior check, w/ nested routes - I'd really appreciate it. > > > > > > > > > > > > Thanks, > > > > > > Daniel > > > > > > > > > > > > On 5/30/07, Chris Anderson wrote: > > > > > > > I usually do something like this in my before(:each) section: > > > > > > > > > > > > > > User.stub!(:find).and_return(@u = mock_model(User, :posts => @pc > = > > > > > > > mock('posts collection'))) > > > > > > > @pc.stub(:find).and_return([@p = mock_model(Post)]) > > > > > > > > > > > > > > with the corresponding should_receives in my various examples... > the > > > > > > > trick is to mock the user's posts collection as its own object. > > > > > > > > > > > > > > On 5/30/07, Fischer, Daniel < daniel at danielfischer.com> wrote: > > > > > > > > Jonathan, > > > > > > > > > > > > > > > > Yeah something like that. I have no idea how to put it > together > > > > > > > > properly. I'm beyond confused at this point, that's why I > could > > > really > > > > > > > > use some help. > > > > > > > > > > > > > > > > I figured out my "response" error, but now Im trying to "find > all > > > posts" > > > > > > > > > > > > > > > > Right now I got something like: > > > > > > > > > > > > > > > > > > > > > > > > > > User.should_receive(:posts).with(:all).and_return([@posts]) > > > > > > > > > > > > > > > > But I know it should be User.posts.find(:all) - i'm not sure > how > > > to > > > > > > > > make this a proper behavior check. > > > > > > > > > > > > > > > > How do I setup my mocks to correspond with this? > > > > > > > > > > > > > > > > Thanks. > > > > > > > > > > > > > > > > On 5/30/07, Jonathan Linowes < jonathan at parkerhill.com> wrote: > > > > > > > > > Don't you need to stub the User model :find too? (needed in > your > > > > > > > > > private get_user call) > > > > > > > > > > > > > > > > > > private > > > > > > > > > def get_user > > > > > > > > > @user = User.find(params[:user_id]) > > > > > > > > > @post = @user.posts.find(params[:id]) if params[:id] > > > > > > > > > end > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On May 30, 2007, at 8:03 PM, Fischer, Daniel wrote: > > > > > > > > > > > > > > > > > > > My problem has been listed here: > > > > > > > > > > > > > > > > > http://railsforum.com/viewtopic.php?pid=25439#p25439 > > > > > > > > > > > > > > > > > > > > Don't think it would be required to completely re-type it > here > > > :) > > > > > > > > > > > > > > > > > > > > Thanks! > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > > > -Daniel Fischer > > > > > > > > > > > > > > > > > > > > http://danielfischer.com - Geek Blog > > > > > > > > > > http://abigfisch.com - Portfolio > > > > > > > > > > http://writersbeat.com - Writing Community > > > > > > > > > > > > > _______________________________________________ > > > > > > > > > > rspec-users mailing list > > > > > > > > > > rspec-users at rubyforge.org > > > > > > > > > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > > > > > rspec-users mailing list > > > > > > > > > rspec-users at rubyforge.org > > > > > > > > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > -Daniel Fischer > > > > > > > > > > > > > > > > http://danielfischer.com - Geek Blog > > > > > > > > http://abigfisch.com - Portfolio > > > > > > > > http://writersbeat.com - Writing Community > > > > > > > > > _______________________________________________ > > > > > > > > rspec-users mailing list > > > > > > > > rspec-users at rubyforge.org > > > > > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > Chris Anderson > > > > > > > http://jchris.mfdz.com > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > > > > > -Daniel Fischer > > > > > > > > > > > > http://danielfischer.com - Geek Blog > > > > > > http://abigfisch.com - Portfolio > > > > > > http://writersbeat.com - Writing Community > > > > > > > > > > > > > > > -- > > > > > -Daniel Fischer > > > > > > > > > > http://danielfischer.com - Geek Blog > > > > > http://abigfisch.com - Portfolio > > > > > http://writersbeat.com - Writing Community > > > > > _______________________________________________ > > > > > rspec-users mailing list > > > > > rspec-users at rubyforge.org > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > -- > > > > > > -Daniel Fischer > > > > > > http://danielfischer.com - Geek Blog > > > http://abigfisch.com - Portfolio > > > http://writersbeat.com - Writing Community > > > > > > -- > > -Daniel Fischer > > http://danielfischer.com - Geek Blog > http://abigfisch.com - Portfolio > http://writersbeat.com - Writing Community -- -Daniel Fischer http://danielfischer.com - Geek Blog http://abigfisch.com - Portfolio http://writersbeat.com - Writing Community From dchelimsky at gmail.com Fri Jun 1 01:00:29 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 1 Jun 2007 01:00:29 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> Message-ID: <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> On 6/1/07, nicholas a. evans wrote: > On 5/31/07, David Chelimsky wrote: > > On 5/31/07, nicholas a. evans wrote: > > > So here's my latest thought-experiment: http://pastie.caboo.se/66478 > > > And also, a simpler multi-field version: http://pastie.caboo.se/66510 > > > I took at a shot at something that feels more like the rest of RSpec. > > It's admittedly more verbose than your examples, but I also think it > > speaks well and aligns with everything else in rspec. WDYT? > > > > http://pastie.caboo.se/66679 > > Oh! I like it a *lot*. Yes, it's more verbose on each example than I > prefer (8 characters plus two indentation levels plus the length of > the block variable). But it does read more fluidly, and in many ways > it looks much simpler. My "default_params" is simply your input to > "given". My "columns" or "field_to_validate", is simply the > argument(s) to "it_should_require_that". Likewise, "as" reads more > naturally than :msg (although I had been thinking of using > "with_message", which feels a bit more readable). > > I have only one real objection/concern: > > "given User.new" would pass in an object that would get reused for > each accept/reject line in the block. I prefer the approach of > creating a new/fresh object for each example. Perhaps it could accept > a Proc or a symbol representing a method name, searching first for an > instance method in the behavior, and then a class method on the class > being described. > > "given :new" would get a different object from User.new before each example. > "given User.new" would reuse the same new User object for all of the examples. > "given :user_with_full_access" would call "user_with_full_access" > (defined in that behavior or on User) before each example. > "given user_with_full_access" would reuse the same object for all examples. > > What do you think? Is there a better way? I put up a third version: http://pastie.caboo.se/66776 WDYT? > A few more thoughts and questions: > > How about "it_should_validate_that" rather than > "it_should_require_that"? "validate" is fine. > That feels more explicit and to-the-point, > to me. At that point, the ":behavior_type => :validation" becomes > redundant. "it_should_validate_that" could simply be available to all > model behaviors. Why not just stick it in a module and let the user decide where to make it available? If you, as a user of this extension, wanted it available to all model examples, you could do this: Spec::Runner.configure do |config| config.include(ModelValidations, :behaviour_type => :model) end > What else could "given" be used for? It seems like this could become > a pattern/approach that could extend beyond model validations... Have > you thought about using it for anything else yet? Not yet. I was just bouncing off your proposal. Let's see how this works in practice before we start creating conventions :) > How could we specify a *default* rejection message (or error count, > etc)? Does "user.default_rejection_message = /the message/" (and so > on) look reasonable? That seems like you're telling the user to use a specific message rather than generally expecting it, which is what I *think* you mean. Am I missing it? > > I did like that the each example/assertion got its own entry in the > specdoc with my approach... Do you think that it is best to sum it up > to the rule? Either way, I presume that a failure should still run > all of examples to give a full report, rather than stop at the first > wrong one. I think you can get all that. The #given method has a handle on the object before it yields to the block, the should_reject and should_accept methods can be configured to create examples at run time. Make sense? > > My approach assumed that if you were only working with one field, we > should only check for errors on that one field (it's okay if the > object is otherwise invalid). But if you are working with multiple > fields, you should check to see if the entire object is valid, and > check against all error messages. Do you think that is a safe and > reasonable convention? This could get quite complicated. Which is probably why nobody has tackled this in a generic way yet - at least that I've seen. > > Thanks for your thoughts... and your improvements on my thoughts. :-) My pleasure. I'm looking forward to seeing how this turns out. Cheers, David > > -- > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From justnothing at tiscali.co.uk Fri Jun 1 05:00:53 2007 From: justnothing at tiscali.co.uk (David Green) Date: Fri, 1 Jun 2007 02:00:53 -0700 (PDT) Subject: [rspec-users] rspec_on_rails, could someone provide an example of using mocks/stubs? Message-ID: <10909256.post@talk.nabble.com> hi all I'm still a little uncertain on how to use mocks/stubs with rspec when dealing with associations. I know the point is to isolate the code being tested and remove external dependencies, but I'm not sure how to implement it. Can someone suggest how they would spec the code below: I have a Book model and Review model class Book < ActiveRecord::Base has_many :reviews def update_rating new_rating = 0.0 unless reviews.empty? reviews.each {|r| new_rating += r.rating} new_rating /= review.size end update_attribute(:rating, new_rating) end end The review class is just a standard model with a :rating attribute. Here's the (empty) spec i came up with: describe Book, ".update_rating" do it "should set rating to 0.0 when there are no reviews" do end it "should set :rating to the average rating when there are reviews" do end end I know I'm supposed to implement the specs first but I wrote the update_attribute() method here to show the kinds of method calls it would be making. Here's what I think I need to do: - stub @book.reviews to return a mock object e.g. mock_reviews - stub mock_reviews.each to return another mock e.g. mock_r - stub mock_r.rating to return some value am I on the right track? it seems like a lot of preparation for such a simple test. Is there a better way? thanks dave -- View this message in context: http://www.nabble.com/rspec_on_rails%2C-could-someone-provide-an-example-of-using-mocks-stubs--tf3851161.html#a10909256 Sent from the rspec-users mailing list archive at Nabble.com. From nick at ekenosen.net Fri Jun 1 10:34:23 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Fri, 1 Jun 2007 10:34:23 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> Message-ID: <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> On 6/1/07, David Chelimsky wrote: > I put up a third version: http://pastie.caboo.se/66776 I looks like you lost the ability to know which field was being validated, so I changed it back to it_should_validate_that :field, "satisfies rule" > Why not just stick it in a module and let the user decide where to > make it available? That makes sense. > Not yet. I was just bouncing off your proposal. Let's see how this > works in practice before we start creating conventions :) Good point. :-) > That seems like you're telling the user to use a specific message > rather than generally expecting it, which is what I *think* you mean. > Am I missing it? You're right... I'll worry about that feature later, then. :-) > I think you can get all that. The #given method has a handle on the > object before it yields to the block, the should_reject and > should_accept methods can be configured to create examples at run > time. Make sense? Yeah, but we've also added a string via "it_should_validate_that"... What would the specdoc look like? We don't have a mechanism for nesting 3 levels deep, do we? > > My approach assumed that if you were only working with one field, we > > should only check for errors on that one field (it's okay if the > > object is otherwise invalid). But if you are working with multiple > > fields, you should check to see if the entire object is valid, and > > check against all error messages. Do you think that is a safe and > > reasonable convention? > > This could get quite complicated. Which is probably why nobody has > tackled this in a generic way yet - at least that I've seen. True... but it feels like we're getting close to something that is very good and simple. It doesn't need to solve 100% of the world's validation needs... just 85% of my rails validation needs. For anything more complicated, I can always fall back on vanilla rspec (still pretty tasty!). For my tastes and intuition, I prefer that validation specs against a single field check only that single field's validity, and validations specs against multiple fields check the model's validity. But I would be fine if it were simplified to normally check only the fields under inspection, but could be easily changed to check for any errors on the object. Here are my next several attempts: Playing off of your approach and including a number of proposed specdoc outputs: http://pastie.caboo.se/66855 But here's my current favorite approach: http://pastie.caboo.se/66862 -- Nick From nick at ekenosen.net Fri Jun 1 11:11:31 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Fri, 1 Jun 2007 11:11:31 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> Message-ID: <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> On 6/1/07, nicholas a. evans wrote: > But here's my current favorite approach: http://pastie.caboo.se/66862 Ack! I had some silly/bad ruby syntax in that last pastie. Let's try that again; now with improved (valid) ruby syntax! http://pastie.caboo.se/66896 -- Nick From tristil at gmail.com Fri Jun 1 12:17:31 2007 From: tristil at gmail.com (Joseph Method) Date: Fri, 1 Jun 2007 12:17:31 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> Message-ID: <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> What about 'this' as a reserved name for what's given? given { User.new } this.should_reject (nil) given { User.new(:name => 'Bob' } this.should_reject(nil) On 6/1/07, nicholas a. evans wrote: > On 6/1/07, nicholas a. evans wrote: > > But here's my current favorite approach: http://pastie.caboo.se/66862 > > Ack! I had some silly/bad ruby syntax in that last pastie. > > Let's try that again; now with improved (valid) ruby syntax! > > http://pastie.caboo.se/66896 > > -- > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- -J. Method From nick at ekenosen.net Fri Jun 1 13:02:24 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Fri, 1 Jun 2007 13:02:24 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> Message-ID: <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> On 6/1/07, Joseph Method wrote: > What about 'this' as a reserved name for what's given? I thought about trying something like that, and thus try to get back to the "normal" rspec syntax but it seems to me that "it" better fulfills that role. Here's my thought on a syntax like that: http://pastie.caboo.se/66937 How close does that compare to what you were thinking of, Joseph? (I prefer to use "specify" rather than "it", when I'm using the generated example string.) Now that I look at it, I actually think this is a rather nice syntax. But it's a lot more verbose than I'd like... perhaps "it_should_reject(foo)" could simply be shorthand for "specify { it.should reject(foo) }". This also makes it obvious how to override the generated example string. Thoughts? Is it an evil or difficult thing to repurpose "it" to my own dubious ends inside the examples? I really like the idea of "it" simply returning the result of the block that was previously passed to "given" -- Nick > given { User.new } > this.should_reject (nil) > > given { User.new(:name => 'Bob' } > this.should_reject(nil) From tristil at gmail.com Fri Jun 1 13:19:28 2007 From: tristil at gmail.com (Joseph Method) Date: Fri, 1 Jun 2007 13:19:28 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> Message-ID: <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> Well, this is what I was thinking: http://pastie.caboo.se/66940 > Thoughts? Honestly, the extra depth is a big turn-off for me. > Is it an evil or difficult thing to repurpose "it" to my > own dubious ends inside the examples? I really like the idea of "it" > simply returning the result of the block that was previously passed to > "given" > > -- > Nick It's not evil, but I don't know how difficult it is. If it's not difficult at all, I don't see why it can't be it.should_validate :password do -- -J. Method From nick at ekenosen.net Fri Jun 1 14:16:46 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Fri, 1 Jun 2007 14:16:46 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> Message-ID: <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> On 6/1/07, Joseph Method wrote: > Well, this is what I was thinking: http://pastie.caboo.se/66940 I think that I still prefer dedicating the entire behavior to validation of a particular field (that's what I do in my current specs anyway, albeit with far more lines of code). > > Thoughts? > > Honestly, the extra depth is a big turn-off for me. Strongly agreed in that regard. 10 extra characters on the front (and two on the back), repeated over every example. The whole point of this is to clean up the syntactic noise to highlight the examples themselves. The reasons I see to use that approach: it matches *very* well with the standard rspec way of doing things. At that point, it's really just the new given/it syntax, and a matcher that looks at some of the behavior options (to see which fields are being spec'd). And it gives an obvious interface for overriding/ignoring the generated example string. Likewise, it gives a nice syntax if I want to do some more advanced assertions on the model with that example. But I think I would use the shorthand version for 95% of my validation examples. And the shorthand could effectively generate the proposed longhand code. > > Is it an evil or difficult thing to repurpose "it" to my > > own dubious ends inside the examples? I really like the idea of "it" > > simply returning the result of the block that was previously passed to > > "given" > > It's not evil, but I don't know how difficult it is. If it's not > difficult at all, I don't see why it can't be > > it.should_validate :password do I don't think it would be right to have the behavior method "it" running double-duty (it is already used to create the examples). Also, I don't really see much benefit of "it.should_validate" over "it_should_validate". But I do like the idea of the example method "it" getting the results of the "given", and that should be easy (I think). -- Nick From jonathan at parkerhill.com Fri Jun 1 14:31:15 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Fri, 1 Jun 2007 14:31:15 -0400 Subject: [rspec-users] Could anyone please help with rspec/nested resource behavior checking? In-Reply-To: References: <7e565b5c0705301703w334c3e2dg3f05d734ed76cd5a@mail.gmail.com> <1D95C70C-B551-43D3-AE07-5E7FFD9C5B74@parkerhill.com> <7e565b5c0705301807o42711e51w555ace5e8c037c84@mail.gmail.com> Message-ID: <84AC7EEA-6A4E-44B8-B2AD-D41CE5FF4488@parkerhill.com> I haven't thought it through (no pun intended) but maybe this could be simplified and generalized , like add a ".through" to the stub and/ or should_receive ?? On May 31, 2007, at 1:01 AM, Chris Anderson wrote: > I usually do something like this in my before(:each) section: > > User.stub!(:find).and_return(@u = mock_model(User, :posts => @pc = > mock('posts collection'))) > @pc.stub(:find).and_return([@p = mock_model(Post)]) > > with the corresponding should_receives in my various examples... the > trick is to mock the user's posts collection as its own object. > From tristil at gmail.com Fri Jun 1 14:47:04 2007 From: tristil at gmail.com (Joseph Method) Date: Fri, 1 Jun 2007 14:47:04 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705311456p716f9c8mf564f3cd4cbfd40b@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> Message-ID: <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> How about this? http://pastie.caboo.se/66977 The strategy is to check if the string argument is :validations (so this would become reserved). If it is, then include ModelValidations. 'it' without the block returns an object that contains a should method. The block it opens is related to but different from the one that 'it' opens. given doesn't seem strictly necessary, since we know what's being described, but it reads well and is explicit. > I think that I still prefer dedicating the entire behavior to > validation of a particular field (that's what I do in my current specs > anyway, albeit with far more lines of code). I'm sure we won't be able to agree on this, but it seems like overkill to me. What if in this proposal there was an option for :validation_of_password, etc.? > I don't think it would be right to have the behavior method "it" > running double-duty (it is already used to create the examples). > Also, I don't really see much benefit of "it.should_validate" over > "it_should_validate". But I do like the idea of the example method > "it" getting the results of the "given", and that should be easy (I > think). Yeah, what's described here might be too evil. In its defense, though, the new 1.0 syntax seems to emphasize subject.should predicate over subject.should_predicate, which is what I've tried to preserve. > -- > Nick -- -J. Method From dchelimsky at gmail.com Fri Jun 1 18:01:09 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 1 Jun 2007 18:01:09 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <276ff9870705312106u1dd74b9fj1eb3d1e9dec7eeee@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> Message-ID: <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> On 6/1/07, Joseph Method wrote: > How about this? > > http://pastie.caboo.se/66977 > > The strategy is to check if the string argument is :validations (so > this would become reserved). If it is, then include ModelValidations. > 'it' without the block returns an object that contains a should > method. The block it opens is related to but different from the one > that 'it' opens. > > given doesn't seem strictly necessary, since we know what's being > described, but it reads well and is explicit. > > > I think that I still prefer dedicating the entire behavior to > > validation of a particular field (that's what I do in my current specs > > anyway, albeit with far more lines of code). > > I'm sure we won't be able to agree on this, but it seems like overkill > to me. What if in this proposal there was an option for > :validation_of_password, etc.? > > > I don't think it would be right to have the behavior method "it" > > running double-duty (it is already used to create the examples). > > Also, I don't really see much benefit of "it.should_validate" over > > "it_should_validate". But I do like the idea of the example method > > "it" getting the results of the "given", and that should be easy (I > > think). > > Yeah, what's described here might be too evil. In its defense, though, > the new 1.0 syntax seems to emphasize subject.should predicate over > subject.should_predicate, which is what I've tried to preserve. Well now we're getting much simpler. Instead of a whole new structure, why not just use a custom matcher? http://pastie.caboo.se/67069 > > > -- > > Nick > > -- > -J. Method > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From anthony at digitalphenom.com Fri Jun 1 18:20:32 2007 From: anthony at digitalphenom.com (Anthony Carlos) Date: Fri, 1 Jun 2007 18:20:32 -0400 Subject: [rspec-users] Specs for ApplicationController, where to put them? In-Reply-To: <57c63afe0705291652r2bb1ea05o29ed4b4ec96e29ed@mail.gmail.com> References: <2D69A175-3C2A-4E38-A006-68695DF3E359@wincent.com> <050EF0F7-7EE3-4995-AFE3-0FD735F98167@digitalphenom.com> <583D179D-743C-49BB-9CFA-B31AF11C71E2@digitalphenom.com> <57c63afe0705291652r2bb1ea05o29ed4b4ec96e29ed@mail.gmail.com> Message-ID: On May 29, 2007, at 7:52 PM, David Chelimsky wrote: >> My problem now, is that I get a nil object error. Specifically, in >> application.rb, I'm trying to call request.parameters and request is >> nil. It seems that request is not part of the context anymore since I >> moved the behavior out to a separate file. How do I regain access to >> request, response, assigns, flash, and session? > > The fact that the shared examples are in another file should have no > bearing on whether they have runtime access to whatever is in the > behaviours that include them. This all sounds odd to me. Can you post > a backtrace please? David: Here's the output from rake spec:autotest: : QuestionnairesController - should flash notice when session[:current_user_id] does not exist (ERROR - 1) - should return true when session[:current_user_id] exists - should respond to current_user - should respond to authenticate 1) NoMethodError in 'QuestionnairesController should flash notice when session[:current_user_id] does not exist' You have a nil object when you didn't expect it! The error occurred while evaluating nil.parameters /Users/aec/ror/aca/aca_trunk/config/../app/controllers/application.rb: 11:in `authenticate' ./spec/controllers/application_controller_spec.rb:24: ===== Here's application_controller_spec.rb: require File.dirname(__FILE__) + '/../spec_helper' unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers") describe "All controllers", :shared => true do it "should respond to authenticate" do controller.should respond_to(:authenticate) end it "should respond to current_user" do controller.should respond_to(:current_user) end it "should return true when session[:current_user_id] exists" do session[:current_user_id] = 12345 controller.authenticate.should == true end it "should flash notice when session[:current_user_id] does not exist" do session[:current_user_id] = nil controller.authenticate # <===== This is line 24 where the error occurs! flash[:notice].should == 'Please login.' end end end ===== Here's application.rb: class ApplicationController < ActionController::Base # Pick a unique cookie name to distinguish our session data from others' session :session_key => '_unique_session_id' def authenticate unless session[:current_user_id] flash[:notice] = 'Please login.' session[:jumpto] = request.parameters # <===== This is line 11 where nil.parameters is called! redirect_to(:controller => 'ums', :action => 'login') end true end def current_user @current_user = User.find(session[:current_user_id]) end end ===== Finally, here's questionnaires_controller_spec.rb: require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/application_controller_spec' unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers") describe QuestionnairesController do it_should_behave_like "All controllers" end : ===== So as far as I can tell, my QuestionnairesController should behave like "All controllers" which puts us at application_controller_spec.rb. The first 3 examples pass, but the fourth, which calls ApplicationController.authenticate, gives me the nil object. But, isn't request an object that is always available to controllers? Wait a second... There's no get or post command here! Maybe thats why request is nil? I think so. Never mind! Sorry about the false alarm. I don't want to be the boy who cried wolf. On the other hand, I think I avoided using get or post because this is an abstract action that gets called by other concrete classes. So how does one test logic in an abstract class? Smells like a job for a mock, of course?!? I'll look over the list archive-- I think other people have asked this question before. Thanks for helping out the slow learners, -Anthony From aslak.hellesoy at gmail.com Fri Jun 1 18:59:23 2007 From: aslak.hellesoy at gmail.com (aslak hellesoy) Date: Sat, 2 Jun 2007 00:59:23 +0200 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> Message-ID: <8d961d900706011559s1056a929i2a64758e2aa577e8@mail.gmail.com> On 6/2/07, David Chelimsky wrote: > On 6/1/07, Joseph Method wrote: > > How about this? > > > > http://pastie.caboo.se/66977 > > > > The strategy is to check if the string argument is :validations (so > > this would become reserved). If it is, then include ModelValidations. > > 'it' without the block returns an object that contains a should > > method. The block it opens is related to but different from the one > > that 'it' opens. > > > > given doesn't seem strictly necessary, since we know what's being > > described, but it reads well and is explicit. > > > > > I think that I still prefer dedicating the entire behavior to > > > validation of a particular field (that's what I do in my current specs > > > anyway, albeit with far more lines of code). > > > > I'm sure we won't be able to agree on this, but it seems like overkill > > to me. What if in this proposal there was an option for > > :validation_of_password, etc.? > > > > > I don't think it would be right to have the behavior method "it" > > > running double-duty (it is already used to create the examples). > > > Also, I don't really see much benefit of "it.should_validate" over > > > "it_should_validate". But I do like the idea of the example method > > > "it" getting the results of the "given", and that should be easy (I > > > think). > > > > Yeah, what's described here might be too evil. In its defense, though, > > the new 1.0 syntax seems to emphasize subject.should predicate over > > subject.should_predicate, which is what I've tried to preserve. > > Well now we're getting much simpler. Instead of a whole new structure, > why not just use a custom matcher? > > http://pastie.caboo.se/67069 > Brilliant! Aslak > > > > > -- > > > Nick > > > > -- > > -J. Method > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From luislavena at gmail.com Fri Jun 1 19:40:59 2007 From: luislavena at gmail.com (Luis Lavena) Date: Fri, 1 Jun 2007 20:40:59 -0300 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <8d961d900706011559s1056a929i2a64758e2aa577e8@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> <8d961d900706011559s1056a929i2a64758e2aa577e8@mail.gmail.com> Message-ID: <71166b3b0706011640i4c731402td3f180b83952b24f@mail.gmail.com> On 6/1/07, aslak hellesoy wrote: > > On 6/1/07, Joseph Method wrote: > > > > Well now we're getting much simpler. Instead of a whole new structure, > > why not just use a custom matcher? > > > > http://pastie.caboo.se/67069 > > > > Brilliant! > I agree with aslak on this! Is descriptive, but not too verbose, is simple but not too cryptic. Translation: Looks good, are you willing to share it? ;-) (David and Aslak, I didn't found a example on how to implemente a custom matcher like Jospeh did, guess I missed a lot of RDocs :-P -- Luis Lavena Multimedia systems - Leaders are made, they are not born. They are made by hard effort, which is the price which all of us must pay to achieve any goal that is worthwhile. Vince Lombardi From dchelimsky at gmail.com Fri Jun 1 19:48:59 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 1 Jun 2007 19:48:59 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <71166b3b0706011640i4c731402td3f180b83952b24f@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> <8d961d900706011559s1056a929i2a64758e2aa577e8@mail.gmail.com> <71166b3b0706011640i4c731402td3f180b83952b24f@mail.gmail.com> Message-ID: <57c63afe0706011648l7b96493cv6cabb962e0b21d0a@mail.gmail.com> On 6/1/07, Luis Lavena wrote: > On 6/1/07, aslak hellesoy wrote: > > > On 6/1/07, Joseph Method wrote: > > > > > > Well now we're getting much simpler. Instead of a whole new structure, > > > why not just use a custom matcher? > > > > > > http://pastie.caboo.se/67069 > > > > > > > Brilliant! > > > > I agree with aslak on this! > > Is descriptive, but not too verbose, is simple but not too cryptic. > > Translation: Looks good, are you willing to share it? ;-) > > (David and Aslak, I didn't found a example on how to implemente a > custom matcher like Jospeh did, guess I missed a lot of RDocs :-P Check this out - I think it's what you're looking for. http://blog.davidchelimsky.net/articles/2007/02/18/custom-expectation-matchers Cheers, David > > -- > Luis Lavena > Multimedia systems > - > Leaders are made, they are not born. They are made by hard effort, > which is the price which all of us must pay to achieve any goal that > is worthwhile. > Vince Lombardi > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From luislavena at gmail.com Fri Jun 1 19:54:32 2007 From: luislavena at gmail.com (Luis Lavena) Date: Fri, 1 Jun 2007 20:54:32 -0300 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <57c63afe0706011648l7b96493cv6cabb962e0b21d0a@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> <8d961d900706011559s1056a929i2a64758e2aa577e8@mail.gmail.com> <71166b3b0706011640i4c731402td3f180b83952b24f@mail.gmail.com> <57c63afe0706011648l7b96493cv6cabb962e0b21d0a@mail.gmail.com> Message-ID: <71166b3b0706011654h48e3b72ft2709a3c12441da9e@mail.gmail.com> On 6/1/07, David Chelimsky wrote: [...] > > > > (David and Aslak, I didn't found a example on how to implemente a > > custom matcher like Jospeh did, guess I missed a lot of RDocs :-P > > Check this out - I think it's what you're looking for. > > http://blog.davidchelimsky.net/articles/2007/02/18/custom-expectation-matchers > Thanks David, but I'm already a subscriber of your blog ;-) What I really meant to say is the .as(:attribute).because("message") part of the custom matcher I didn't fully understand :-P Are examples of that in the repository? -- Luis Lavena Multimedia systems - Leaders are made, they are not born. They are made by hard effort, which is the price which all of us must pay to achieve any goal that is worthwhile. Vince Lombardi From dchelimsky at gmail.com Sat Jun 2 00:19:20 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 2 Jun 2007 00:19:20 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <71166b3b0706011654h48e3b72ft2709a3c12441da9e@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> <8d961d900706011559s1056a929i2a64758e2aa577e8@mail.gmail.com> <71166b3b0706011640i4c731402td3f180b83952b24f@mail.gmail.com> <57c63afe0706011648l7b96493cv6cabb962e0b21d0a@mail.gmail.com> <71166b3b0706011654h48e3b72ft2709a3c12441da9e@mail.gmail.com> Message-ID: <57c63afe0706012119v6195e4f4y112fe157e9797dfc@mail.gmail.com> On 6/1/07, Luis Lavena wrote: > On 6/1/07, David Chelimsky wrote: > [...] > > > > > > (David and Aslak, I didn't found a example on how to implemente a > > > custom matcher like Jospeh did, guess I missed a lot of RDocs :-P > > > > Check this out - I think it's what you're looking for. > > > > http://blog.davidchelimsky.net/articles/2007/02/18/custom-expectation-matchers > > > > Thanks David, but I'm already a subscriber of your blog ;-) > > What I really meant to say is the .as(:attribute).because("message") > part of the custom matcher I didn't fully understand :-P > > Are examples of that in the repository? Take a look at lib/spec/matchers/change.rb. That should give you some direction. > > -- > Luis Lavena > Multimedia systems > - > Leaders are made, they are not born. They are made by hard effort, > which is the price which all of us must pay to achieve any goal that > is worthwhile. > Vince Lombardi > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From daniel at danielfischer.com Sat Jun 2 04:27:32 2007 From: daniel at danielfischer.com (Fischer, Daniel) Date: Sat, 2 Jun 2007 01:27:32 -0700 Subject: [rspec-users] I'm really bad at controllers, help please. Message-ID: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> Hey, Sorry for so many questions - I'm really bad at this right now. I'm trying to cover the following code w/ rspec def index if params[:user_id] @user = User.find(params[:user_id]) @messages = @user.messages end end So basically what I'm doing is listing all the messages for a user, provided there is an id parameter. describe MessagesController, " handling GET /messages for a user" do before do @message = mock_model(Message) @message.stub!(:user_id).and_return(1) @user = mock_model(User) @user.stub!(:id).and_return(1) User.stub!(:messages).and_return([@message]) User.stub!(:find).and_return([@user]) end def do_get get :index, :user_id => 1 end it "should be successful" do do_get response.should be_success end it "should render index template" do do_get response.should render_template('index') end it "should find all messages" do User.should_receive(:messages).and_return([@message]) do_get end it "should assign the found messages for the view" do do_get assigns[:messages].should == [@message] end end I'm trying to use the basic scaffold spec, but I'm absolutely clueless on what the proper way to handle this is, I'm not even sure the proper way I should mock the messages method, so it'll return a "stub?" of a collection, or whatever the proper term is. I'm sorry you have to deal with a newb, but if you could really help me out I'd appreciate it a ton, thanks! -- -Daniel Fischer From arachnd at gmail.com Sat Jun 2 04:29:47 2007 From: arachnd at gmail.com (Fischer, Daniel) Date: Sat, 2 Jun 2007 01:29:47 -0700 Subject: [rspec-users] I'm really bad at controllers, help please. Message-ID: <7e565b5c0706020129k594e1be3r9b6230e70ec4c911@mail.gmail.com> Hey, Sorry for so many questions - I'm really bad at this right now. I'm trying to cover the following code w/ rspec def index if params[:user_id] @user = User.find(params[:user_id]) @messages = @user.messages end end So basically what I'm doing is listing all the messages for a user, provided there is an id parameter. describe MessagesController, " handling GET /messages for a user" do before do @message = mock_model(Message) @message.stub!(:user_id).and_return(1) @user = mock_model(User) @user.stub!(:id).and_return(1) User.stub!(:messages).and_return([@message]) User.stub!(:find).and_return([@user]) end def do_get get :index, :user_id => 1 end it "should be successful" do do_get response.should be_success end it "should render index template" do do_get response.should render_template('index') end it "should find all messages" do User.should_receive(:messages).and_return([@message]) do_get end it "should assign the found messages for the view" do do_get assigns[:messages].should == [@message] end end I'm trying to use the basic scaffold spec, but I'm absolutely clueless on what the proper way to handle this is, I'm not even sure the proper way I should mock the messages method, so it'll return a "stub?" of a collection, or whatever the proper term is. I'm sorry you have to deal with a newb, but if you could really help me out I'd appreciate it a ton, thanks! -Daniel Fischer http://www.danielfischer.com From daniel at danielfischer.com Sat Jun 2 04:31:21 2007 From: daniel at danielfischer.com (Fischer, Daniel) Date: Sat, 2 Jun 2007 01:31:21 -0700 Subject: [rspec-users] Could anyone please help with rspec/nested resource behavior checking? In-Reply-To: <84AC7EEA-6A4E-44B8-B2AD-D41CE5FF4488@parkerhill.com> References: <7e565b5c0705301703w334c3e2dg3f05d734ed76cd5a@mail.gmail.com> <1D95C70C-B551-43D3-AE07-5E7FFD9C5B74@parkerhill.com> <7e565b5c0705301807o42711e51w555ace5e8c037c84@mail.gmail.com> <84AC7EEA-6A4E-44B8-B2AD-D41CE5FF4488@parkerhill.com> Message-ID: <7e565b5c0706020131p11d0390didc67fb1eacf1d0ee@mail.gmail.com> Jonathon, Not sure - I don't really know what to do at this point, but I have a new problem again lol. Anyway, I can't seem to create new mailing messages? I guess I ask to many... Well, forgive me - but I'll just attach it here since I can't make a new one. Hey, Sorry for so many questions - I'm really bad at this right now. I'm trying to cover the following code w/ rspec def index if params[:user_id] @user = User.find(params[:user_id]) @messages = @user.messages end end So basically what I'm doing is listing all the messages for a user, provided there is an id parameter. describe MessagesController, " handling GET /messages for a user" do before do @message = mock_model(Message) @message.stub!(:user_id).and_return(1) @user = mock_model(User) @user.stub!(:id).and_return(1) User.stub!(:messages).and_return([@message]) User.stub!(:find).and_return([@user]) end def do_get get :index, :user_id => 1 end it "should be successful" do do_get response.should be_success end it "should render index template" do do_get response.should render_template('index') end it "should find all messages" do User.should_receive(:messages).and_return([@message]) do_get end it "should assign the found messages for the view" do do_get assigns[:messages].should == [@message] end end I'm trying to use the basic scaffold spec, but I'm absolutely clueless on what the proper way to handle this is, I'm not even sure the proper way I should mock the messages method, so it'll return a "stub?" of a collection, or whatever the proper term is. I'm sorry you have to deal with a newb, but if you could really help me out I'd appreciate it a ton, thanks! On 6/1/07, Jonathan Linowes wrote: > > I haven't thought it through (no pun intended) but maybe this could > be simplified and generalized , like add a ".through" to the stub and/ > or should_receive > ?? > > > On May 31, 2007, at 1:01 AM, Chris Anderson wrote: > > > I usually do something like this in my before(:each) section: > > > > User.stub!(:find).and_return(@u = mock_model(User, :posts => @pc = > > mock('posts collection'))) > > @pc.stub(:find).and_return([@p = mock_model(Post)]) > > > > with the corresponding should_receives in my various examples... the > > trick is to mock the user's posts collection as its own object. > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- -Daniel Fischer http://danielfischer.com - Geek Blog http://abigfisch.com - Portfolio http://writersbeat.com - Writing Community From dylans at gmail.com Fri Jun 1 19:30:19 2007 From: dylans at gmail.com (Dylan Stamat) Date: Fri, 1 Jun 2007 16:30:19 -0700 Subject: [rspec-users] redirect_to a relative path Message-ID: In a controller spec, doing something like: response.redirect_url.should == omglolsrofls_path ... fails, as so: expected "/omglolrofls", got "http://test.host/omglolrofls" (using ==) You can see that the http://test.host is prepended to the path. My controller is indeed redirecting to the omglolrofls path, and not the url. Is this expected ? Oh... and that's an arbitrary controller name if you were wondering ;) Thanks ! == Dylan From dchelimsky at gmail.com Sat Jun 2 09:25:19 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 2 Jun 2007 09:25:19 -0400 Subject: [rspec-users] redirect_to a relative path In-Reply-To: References: Message-ID: <57c63afe0706020625j355c2fe5gb78466c672948e46@mail.gmail.com> On 6/1/07, Dylan Stamat wrote: > In a controller spec, doing something like: > response.redirect_url.should == omglolsrofls_path > > ... fails, as so: > expected "/omglolrofls", got "http://test.host/omglolrofls" (using ==) > > You can see that the http://test.host is prepended to the path. > My controller is indeed redirecting to the omglolrofls path, and not > the url. > Is this expected ? That's how it's doc'd (http://rspec.rubyforge.org/rdoc-rails/classes/Spec/Rails/Matchers.html#M000014). You can use omglolsrofls_url instead. If you think this should behave differently, feel free to submit a feature request: http://rubyforge.org/tracker/?group_id=797 Cheers (and welcome), David > > Oh... and that's an arbitrary controller name if you were wondering ;) > Thanks ! > == > Dylan > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From nick at ekenosen.net Sat Jun 2 10:32:51 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Sat, 2 Jun 2007 10:32:51 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <57c63afe0705312200y274a4f43x966600bce6885d5a@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> Message-ID: <276ff9870706020732y511e93aahd50b0fad62969fda@mail.gmail.com> On 6/1/07, David Chelimsky wrote: > Well now we're getting much simpler. Instead of a whole new structure, > why not just use a custom matcher? > > http://pastie.caboo.se/67069 I agree, simpler is better. I intend to start off with just the matcher, and then move towards a shortcut syntax. My nitpick with only doing a matcher and not doing some extras as well: it's still too verbose for my taste. For example, I'll be saying "as(:foo)" for every single example within a behavior, or wrapping it inside some examples_array.each loops will feel a bit clunky. The whole point was to cut away as much of the noise as possible, and focus only on the examples (and occasionally, the error messages). At any rate, I just put together my first draft implementation (developed spec-first). It still doesn't check the error messages yet, and it's only handling single fields at the moment, but I'll do those after I get home from the market. After that I'll figure out how to get it working with my desired "shortcut" syntax. Then I'll package it up all nicely for anyone to use... it's all in one file at the moment, so if you are looking to see the syntax that it enables, you'll have to scroll down to line 109. ;-) http://pastie.caboo.se/67222 I've included the output from "spec -f s" in the pastie. -- Nick From jonathan at parkerhill.com Sat Jun 2 15:09:31 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Sat, 2 Jun 2007 15:09:31 -0400 Subject: [rspec-users] I'm really bad at controllers, help please. In-Reply-To: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> References: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> Message-ID: <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> I'm nearly as new at this as you are I'm I'm just figuring things out myself too. I think your problem is method :messages is being called on an instance of User, not the class, so you need @user.stub!(:messages).and_return([@message]) here's some clues to how I've gotten similar things working To diagnose problems with my examples, I - watch the test.log file. You could use 'tail' or on mac use the 'console' utility - use the -e option in spec to run one example at a time. I copy/ paste the test name from the terminal window to avoid a lot of typing - sometimes I comment out code in my app methods to limit what its actually doing and then add one line at a time to "re-develop" the method - similarly I may create an extra test method in my controller or model to isolate things I dont understand between the spec and the code. I also tend to use should_receive rather than stubs, it provides more details (although seems to make the tests more brittle) thus, # separate for reuse with multiple describe cases def setup_messages do @user = mock_model(User) @message = mock_model(Message) @messages = [@message] end def before(:each) do setup_messages User.should_receive(:find).with("1").and_return(@user) @user.should_receive(:messages).and_return(@messages) end def do_get get :index, :user_id => 1 end it "should find all messages" do do_get assigns[:messages].should == @messages end I think this is right. Please let me know :) On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote: > Hey, > > Sorry for so many questions - I'm really bad at this right now. > > I'm trying to cover the following code w/ rspec > > def index > if params[:user_id] > @user = User.find(params[:user_id]) > @messages = @user.messages > end > end > > So basically what I'm doing is listing all the messages for a user, > provided there is an id parameter. > > describe MessagesController, " handling GET /messages for a user" do > > before do > @message = mock_model(Message) > @message.stub!(:user_id).and_return(1) > @user = mock_model(User) > @user.stub!(:id).and_return(1) > User.stub!(:messages).and_return([@message]) > User.stub!(:find).and_return([@user]) > end > > def do_get > get :index, :user_id => 1 > end > > it "should be successful" do > do_get > response.should be_success > end > > it "should render index template" do > do_get > response.should render_template('index') > end > > it "should find all messages" do > User.should_receive(:messages).and_return([@message]) > do_get > end > > it "should assign the found messages for the view" do > do_get > assigns[:messages].should == [@message] > end > end > > > I'm trying to use the basic scaffold spec, but I'm absolutely clueless > on what the proper way to handle this is, I'm not even sure the proper > way I should mock the messages method, so it'll return a "stub?" of a > collection, or whatever the proper term is. > > I'm sorry you have to deal with a newb, but if you could really help > me out I'd appreciate it a ton, thanks! > -- > -Daniel Fischer > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From jonathan at parkerhill.com Sat Jun 2 23:17:01 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Sat, 2 Jun 2007 23:17:01 -0400 Subject: [rspec-users] should_receive.again Message-ID: <9E413F4D-CAB0-445C-9A9C-28B11EAE3343@parkerhill.com> Hi, It appears that if I have 2 should_receives in a row, the latest one overrides the previous one(s). If there isn't one, could we add a way to accumulate them, such as @thing.should_receive(:method).and_return(@value) @thing.should_receive(:method).again.and_return(@value) @thing.should_receive(:method).again.and_return(@value) would be equivalent to @thing.should_receive(:method).times(3).and_return(@value) This would be helpful because I have a setup method that sets up the should_receive once, and then some specific actions may use that method one or more additional times. thanks linoj From jonathan at parkerhill.com Sat Jun 2 23:48:11 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Sat, 2 Jun 2007 23:48:11 -0400 Subject: [rspec-users] I'm really bad at controllers, help please. In-Reply-To: <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> References: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> Message-ID: <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> fyi, i'm finding the should_receive(:messages) to be too brittle because any time I add a call like @user.messages.find or whatever it breaks my spec (requiring I do .twice or .times(N) . IMO the spec shouldn't be so sensitive to implementation of the behavior, so i'm now stubbing the association replaced @user.should_receive(:messages).and_return(@messages) with @user.stub!(:messages).and_return(@messages) On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote: > I'm nearly as new at this as you are I'm I'm just figuring things out > myself too. > > I think your problem is method :messages is being called on an > instance of User, not the class, so you need > > @user.stub!(:messages).and_return([@message]) > > here's some clues to how I've gotten similar things working > To diagnose problems with my examples, I > - watch the test.log file. You could use 'tail' or on mac use the > 'console' utility > - use the -e option in spec to run one example at a time. I copy/ > paste the test name from the terminal window to avoid a lot of typing > - sometimes I comment out code in my app methods to limit what its > actually doing and then add one line at a time to "re-develop" the > method > - similarly I may create an extra test method in my controller or > model to isolate things I dont understand between the spec and the > code. > > I also tend to use should_receive rather than stubs, it provides more > details (although seems to make the tests more brittle) > > thus, > > # separate for reuse with multiple describe cases > def setup_messages do > @user = mock_model(User) > @message = mock_model(Message) > @messages = [@message] > end > > def before(:each) do > setup_messages > User.should_receive(:find).with("1").and_return(@user) > @user.should_receive(:messages).and_return(@messages) > end > > def do_get > get :index, :user_id => 1 > end > > it "should find all messages" do > do_get > assigns[:messages].should == @messages > end > > I think this is right. Please let me know > :) > > > On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote: > >> Hey, >> >> Sorry for so many questions - I'm really bad at this right now. >> >> I'm trying to cover the following code w/ rspec >> >> def index >> if params[:user_id] >> @user = User.find(params[:user_id]) >> @messages = @user.messages >> end >> end >> >> So basically what I'm doing is listing all the messages for a user, >> provided there is an id parameter. >> >> describe MessagesController, " handling GET /messages for a user" do >> >> before do >> @message = mock_model(Message) >> @message.stub!(:user_id).and_return(1) >> @user = mock_model(User) >> @user.stub!(:id).and_return(1) >> User.stub!(:messages).and_return([@message]) >> User.stub!(:find).and_return([@user]) >> end >> >> def do_get >> get :index, :user_id => 1 >> end >> >> it "should be successful" do >> do_get >> response.should be_success >> end >> >> it "should render index template" do >> do_get >> response.should render_template('index') >> end >> >> it "should find all messages" do >> User.should_receive(:messages).and_return([@message]) >> do_get >> end >> >> it "should assign the found messages for the view" do >> do_get >> assigns[:messages].should == [@message] >> end >> end >> >> >> I'm trying to use the basic scaffold spec, but I'm absolutely >> clueless >> on what the proper way to handle this is, I'm not even sure the >> proper >> way I should mock the messages method, so it'll return a "stub?" of a >> collection, or whatever the proper term is. >> >> I'm sorry you have to deal with a newb, but if you could really help >> me out I'd appreciate it a ton, thanks! >> -- >> -Daniel Fischer >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From aslak.hellesoy at gmail.com Sun Jun 3 06:21:18 2007 From: aslak.hellesoy at gmail.com (aslak hellesoy) Date: Sun, 3 Jun 2007 12:21:18 +0200 Subject: [rspec-users] should_receive.again In-Reply-To: <9E413F4D-CAB0-445C-9A9C-28B11EAE3343@parkerhill.com> References: <9E413F4D-CAB0-445C-9A9C-28B11EAE3343@parkerhill.com> Message-ID: <8d961d900706030321l2d5e1d5an953b06ddb40cd721@mail.gmail.com> On 6/3/07, Jonathan Linowes wrote: > Hi, > > It appears that if I have 2 should_receives in a row, the latest one > overrides the previous one(s). > > If there isn't one, could we add a way to accumulate them, such as > > @thing.should_receive(:method).and_return(@value) > @thing.should_receive(:method).again.and_return(@value) > @thing.should_receive(:method).again.and_return(@value) > > would be equivalent to > > @thing.should_receive(:method).times(3).and_return(@value) > > This would be helpful because I have a setup method that sets up the > should_receive once, and then some specific actions may use that > method one or more additional times. > @thing.should_receive(:method).exactly(3).times.and_return(@value) http://rspec.rubyforge.org/rdoc/classes/Spec/Mocks.html Aslak > thanks > linoj > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Sun Jun 3 09:02:37 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 3 Jun 2007 08:02:37 -0500 Subject: [rspec-users] I'm really bad at controllers, help please. In-Reply-To: <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> References: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> Message-ID: <57c63afe0706030602k69f41fe3t910475947884762d@mail.gmail.com> On 6/2/07, Jonathan Linowes wrote: > fyi, i'm finding the should_receive(:messages) to be too brittle > because any time I add a call like @user.messages.find or whatever it > breaks my spec (requiring I do .twice or .times(N) . IMO the spec > shouldn't be so sensitive to implementation of the behavior, so i'm > now stubbing the association > > replaced > @user.should_receive(:messages).and_return(@messages) > with > @user.stub!(:messages).and_return(@messages) FYI - this: @user.stub!(:messages).and_return(@messages) functions in the same way as this: @user.should_receive(:messages).any_number_of_times.and_return(@messages) So you can alleviate the brittleness using should_receive. It's more verbose, BUT, the important thing is your intention. Stubs and mocks mean different things. http://blog.davidchelimsky.net/articles/2006/11/09/tutorial-rspec-stubs-and-mocks Cheers, David > > > > On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote: > > > I'm nearly as new at this as you are I'm I'm just figuring things out > > myself too. > > > > I think your problem is method :messages is being called on an > > instance of User, not the class, so you need > > > > @user.stub!(:messages).and_return([@message]) > > > > here's some clues to how I've gotten similar things working > > To diagnose problems with my examples, I > > - watch the test.log file. You could use 'tail' or on mac use the > > 'console' utility > > - use the -e option in spec to run one example at a time. I copy/ > > paste the test name from the terminal window to avoid a lot of typing > > - sometimes I comment out code in my app methods to limit what its > > actually doing and then add one line at a time to "re-develop" the > > method > > - similarly I may create an extra test method in my controller or > > model to isolate things I dont understand between the spec and the > > code. > > > > I also tend to use should_receive rather than stubs, it provides more > > details (although seems to make the tests more brittle) > > > > thus, > > > > # separate for reuse with multiple describe cases > > def setup_messages do > > @user = mock_model(User) > > @message = mock_model(Message) > > @messages = [@message] > > end > > > > def before(:each) do > > setup_messages > > User.should_receive(:find).with("1").and_return(@user) > > @user.should_receive(:messages).and_return(@messages) > > end > > > > def do_get > > get :index, :user_id => 1 > > end > > > > it "should find all messages" do > > do_get > > assigns[:messages].should == @messages > > end > > > > I think this is right. Please let me know > > :) > > > > > > On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote: > > > >> Hey, > >> > >> Sorry for so many questions - I'm really bad at this right now. > >> > >> I'm trying to cover the following code w/ rspec > >> > >> def index > >> if params[:user_id] > >> @user = User.find(params[:user_id]) > >> @messages = @user.messages > >> end > >> end > >> > >> So basically what I'm doing is listing all the messages for a user, > >> provided there is an id parameter. > >> > >> describe MessagesController, " handling GET /messages for a user" do > >> > >> before do > >> @message = mock_model(Message) > >> @message.stub!(:user_id).and_return(1) > >> @user = mock_model(User) > >> @user.stub!(:id).and_return(1) > >> User.stub!(:messages).and_return([@message]) > >> User.stub!(:find).and_return([@user]) > >> end > >> > >> def do_get > >> get :index, :user_id => 1 > >> end > >> > >> it "should be successful" do > >> do_get > >> response.should be_success > >> end > >> > >> it "should render index template" do > >> do_get > >> response.should render_template('index') > >> end > >> > >> it "should find all messages" do > >> User.should_receive(:messages).and_return([@message]) > >> do_get > >> end > >> > >> it "should assign the found messages for the view" do > >> do_get > >> assigns[:messages].should == [@message] > >> end > >> end > >> > >> > >> I'm trying to use the basic scaffold spec, but I'm absolutely > >> clueless > >> on what the proper way to handle this is, I'm not even sure the > >> proper > >> way I should mock the messages method, so it'll return a "stub?" of a > >> collection, or whatever the proper term is. > >> > >> I'm sorry you have to deal with a newb, but if you could really help > >> me out I'd appreciate it a ton, thanks! > >> -- > >> -Daniel Fischer > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From martin.emde at gmail.com Sun Jun 3 15:07:21 2007 From: martin.emde at gmail.com (Martin Emde) Date: Sun, 3 Jun 2007 13:07:21 -0600 Subject: [rspec-users] Could anyone please help with rspec/nested resource behavior checking? In-Reply-To: <7e565b5c0706020131p11d0390didc67fb1eacf1d0ee@mail.gmail.com> References: <7e565b5c0705301703w334c3e2dg3f05d734ed76cd5a@mail.gmail.com> <1D95C70C-B551-43D3-AE07-5E7FFD9C5B74@parkerhill.com> <7e565b5c0705301807o42711e51w555ace5e8c037c84@mail.gmail.com> <84AC7EEA-6A4E-44B8-B2AD-D41CE5FF4488@parkerhill.com> <7e565b5c0706020131p11d0390didc67fb1eacf1d0ee@mail.gmail.com> Message-ID: <145359ad0706031207w1e0ba44ajd77d6b3bc54070d2@mail.gmail.com> Here is the correct spec for this index action: def index if params[:user_id] @user = User.find(params[:user_id]) @messages = @user.messages end end describe MessagesController, " handling GET /messages for a user" do before do @user = mock_model(User) @messages = mock("messages") @user.stub!(:messages).and_return(@messages) User.stub!(:find).and_return(@user) end def do_get get :index, :user_id => 1 end it "should render index template" do do_get response.should render_template('index') end it "should find user with params[:user_id]" do User.should_receive(:find).with(1).and_return(@user) do_get end it "should get user's messages" do @user.should_receive(:messages).and_return(@messages) do_get end it "should assign the found messages for the view" do do_get assigns[:messages].should be(@messages) end end Hope that helps you out. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070603/011115a7/attachment.html From dchelimsky at gmail.com Sun Jun 3 15:55:50 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 3 Jun 2007 14:55:50 -0500 Subject: [rspec-users] Specs for ApplicationController, where to put them? In-Reply-To: References: <2D69A175-3C2A-4E38-A006-68695DF3E359@wincent.com> <050EF0F7-7EE3-4995-AFE3-0FD735F98167@digitalphenom.com> <583D179D-743C-49BB-9CFA-B31AF11C71E2@digitalphenom.com> <57c63afe0705291652r2bb1ea05o29ed4b4ec96e29ed@mail.gmail.com> Message-ID: <57c63afe0706031255g4f96cf7h1263e4b067167b26@mail.gmail.com> On 6/1/07, Anthony Carlos wrote: > > On May 29, 2007, at 7:52 PM, David Chelimsky wrote: > > >> My problem now, is that I get a nil object error. Specifically, in > >> application.rb, I'm trying to call request.parameters and request is > >> nil. It seems that request is not part of the context anymore since I > >> moved the behavior out to a separate file. How do I regain access to > >> request, response, assigns, flash, and session? > > > > The fact that the shared examples are in another file should have no > > bearing on whether they have runtime access to whatever is in the > > behaviours that include them. This all sounds odd to me. Can you post > > a backtrace please? > > David: > > Here's the output from rake spec:autotest: > > : > QuestionnairesController > - should flash notice when session[:current_user_id] does not exist > (ERROR - 1) > - should return true when session[:current_user_id] exists > - should respond to current_user > - should respond to authenticate > > 1) > NoMethodError in 'QuestionnairesController should flash notice when > session[:current_user_id] does not exist' > You have a nil object when you didn't expect it! > The error occurred while evaluating nil.parameters > /Users/aec/ror/aca/aca_trunk/config/../app/controllers/application.rb: > 11:in `authenticate' > ./spec/controllers/application_controller_spec.rb:24: > > ===== > Here's application_controller_spec.rb: > require File.dirname(__FILE__) + '/../spec_helper' > > unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers") > describe "All controllers", :shared => true do > it "should respond to authenticate" do > controller.should respond_to(:authenticate) > end > it "should respond to current_user" do > controller.should respond_to(:current_user) > end > it "should return true when session[:current_user_id] exists" do > session[:current_user_id] = 12345 > controller.authenticate.should == true > end > it "should flash notice when session[:current_user_id] does not > exist" do > session[:current_user_id] = nil > controller.authenticate # <===== This is line 24 where the > error occurs! > flash[:notice].should == 'Please login.' > end > end > end > > ===== > Here's application.rb: > class ApplicationController < ActionController::Base > # Pick a unique cookie name to distinguish our session data from > others' > session :session_key => '_unique_session_id' > > def authenticate > unless session[:current_user_id] > flash[:notice] = 'Please login.' > session[:jumpto] = request.parameters # <===== This is line 11 > where nil.parameters is called! > redirect_to(:controller => 'ums', :action => 'login') > end > true > end > > def current_user > @current_user = User.find(session[:current_user_id]) > end > end > > ===== > Finally, here's questionnaires_controller_spec.rb: > require File.dirname(__FILE__) + '/../spec_helper' > require File.dirname(__FILE__) + '/application_controller_spec' > unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers") > > describe QuestionnairesController do > it_should_behave_like "All controllers" > end > : > > ===== > So as far as I can tell, my QuestionnairesController should behave > like "All controllers" which puts us at > application_controller_spec.rb. The first 3 examples pass, but the > fourth, which calls ApplicationController.authenticate, gives me the > nil object. But, isn't request an object that is always available to > controllers? > > Wait a second... > > There's no get or post command here! Maybe thats why request is nil? > I think so. Never mind! Sorry about the false alarm. I don't want to > be the boy who cried wolf. > > On the other hand, I think I avoided using get or post because this > is an abstract action that gets called by other concrete classes. So > how does one test logic in an abstract class? This question inspired me to write this blog post: http://blog.davidchelimsky.net/articles/2007/06/03/oxymoron-testing-behaviour-of-abstractions Feel free to comment directly on the blog. Cheers, David > Smells like a job for a > mock, of course?!? I'll look over the list archive-- I think other > people have asked this question before. > > Thanks for helping out the slow learners, > > -Anthony > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From nick at ekenosen.net Sun Jun 3 22:19:54 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Sun, 3 Jun 2007 22:19:54 -0400 Subject: [rspec-users] should_receive.again In-Reply-To: <8d961d900706030321l2d5e1d5an953b06ddb40cd721@mail.gmail.com> References: <9E413F4D-CAB0-445C-9A9C-28B11EAE3343@parkerhill.com> <8d961d900706030321l2d5e1d5an953b06ddb40cd721@mail.gmail.com> Message-ID: <276ff9870706031919g76913dbdm4a0676b1fd714148@mail.gmail.com> On 6/3/07, aslak hellesoy wrote: > On 6/3/07, Jonathan Linowes wrote: > > This would be helpful because I have a setup method that sets up the > > should_receive once, and then some specific actions may use that > > method one or more additional times. > > @thing.should_receive(:method).exactly(3).times.and_return(@value) > http://rspec.rubyforge.org/rdoc/classes/Spec/Mocks.html You might also consider simply stubbing the method in your setup, and only specifying a number of times called in specific examples. Whenever reasonable, I try to follow the advice given by David Chelimsky and aim for no mock expectations in the setup and only one per example: http://blog.davidchelimsky.net/articles/2006/11/09/tutorial-rspec-stubs-and-mocks Also, you might want to think about whether or not you really should be locking down your specification to exactly three (or however many) calls. Without knowing your details I can't say for certain if this applies to you, but often the exact number of calls is an implementation detail that your spec doesn't need to worry about. If that's the case, I might loosen up the mock expectation a little bit by simply saying @thing.should_receive(:method).at_least(:once).and_return(@value) -- Nick From nick at ekenosen.net Sun Jun 3 22:52:04 2007 From: nick at ekenosen.net (nicholas a. evans) Date: Sun, 3 Jun 2007 22:52:04 -0400 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870706020732y511e93aahd50b0fad62969fda@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <276ff9870706010734q601c018bvc82dc1196d9da00c@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> <276ff9870706020732y511e93aahd50b0fad62969fda@mail.gmail.com> Message-ID: <276ff9870706031952n67e5507clb22837e1da1cc3bb@mail.gmail.com> On 6/2/07, nicholas a. evans wrote: > At any rate, I just put together my first draft implementation > (developed spec-first). It still doesn't check the error messages > yet, and it's only handling single fields at the moment, but I'll do > those after I get home from the market. After that I'll figure out > how to get it working with my desired "shortcut" syntax. So I haven't gotten around to my preferred shortcut syntax yet, but I did add support to check for error messages and handle multiple fields, so it should be quite usable now. I took the approach that when specifying single field validations it only checks for errors on that field, but when specifying multi-field validations it checks for any errors on the object. That felt like simplest and least surprising solution to me. My specs assume that you can use sqlite3 (to do an in-memory DB). http://svn.ekenosen.net/public/rspec_validation_matcher/ http://pastie.caboo.se/67509 shows what it currently can do, as well as my current thoughts on how I would *really* like to be doing this. I'm eager to hear any comments on it. :-) -- Nick From anthony at digitalphenom.com Sun Jun 3 23:44:54 2007 From: anthony at digitalphenom.com (Anthony Carlos) Date: Sun, 3 Jun 2007 23:44:54 -0400 Subject: [rspec-users] Could anyone please help with rspec/nested resource behavior checking? In-Reply-To: <7e565b5c0706020131p11d0390didc67fb1eacf1d0ee@mail.gmail.com> References: <7e565b5c0705301703w334c3e2dg3f05d734ed76cd5a@mail.gmail.com> <1D95C70C-B551-43D3-AE07-5E7FFD9C5B74@parkerhill.com> <7e565b5c0705301807o42711e51w555ace5e8c037c84@mail.gmail.com> <84AC7EEA-6A4E-44B8-B2AD-D41CE5FF4488@parkerhill.com> <7e565b5c0706020131p11d0390didc67fb1eacf1d0ee@mail.gmail.com> Message-ID: <56384D1F-D586-4126-B7CE-50E36F79434E@digitalphenom.com> Daniel, Which of these examples is failing? Or, are you asking a more general question about how to use a mock or a stub? I'm going through my own pains trying to learn this stuff, so my perspective might be close to yours! Please scroll down; I have a couple of in-line questions for ya! On Jun 2, 2007, at 4:31 AM, Fischer, Daniel wrote: > Jonathon, > > Not sure - I don't really know what to do at this point, but I have a > new problem again lol. > > Anyway, I can't seem to create new mailing messages? I guess I ask > to many... > > Well, forgive me - but I'll just attach it here since I can't make > a new one. > > Hey, > > Sorry for so many questions - I'm really bad at this right now. > > I'm trying to cover the following code w/ rspec > > def index > if params[:user_id] > @user = User.find(params[:user_id]) > @messages = @user.messages > end > end > > So basically what I'm doing is listing all the messages for a user, > provided there is an id parameter. > > describe MessagesController, " handling GET /messages for a user" do > > before do > @message = mock_model(Message) > @message.stub!(:user_id).and_return(1) > @user = mock_model(User) > @user.stub!(:id).and_return(1) > User.stub!(:messages).and_return([@message]) > User.stub!(:find).and_return([@user]) > end > > def do_get > get :index, :user_id => 1 > end > > it "should be successful" do > do_get > response.should be_success > end > > it "should render index template" do > do_get > response.should render_template('index') > end > > it "should find all messages" do > User.should_receive(:messages).and_return([@message]) > do_get > end > > it "should assign the found messages for the view" do > do_get > assigns[:messages].should == [@message] > end > end > > > I'm trying to use the basic scaffold spec, but I'm absolutely clueless > on what the proper way to handle this is, I'm not even sure the proper > way I should mock the messages method, so it'll return a "stub?" of a > collection, or whatever the proper term is. > Can you tell me where you found the basic scaffold spec? I haven't seen it yet. I've been learning by studying the docs from the rspec site. Are you having a problem ensuring that the @messages variable in your controller has an array of messages after a GET? I think I can explain the use of a stub or mock if this is really your question. Thanks, -Anthony From jonathan at parkerhill.com Sun Jun 3 23:47:59 2007 From: jonathan at parkerhill.com (Jonathan Linowes) Date: Sun, 3 Jun 2007 23:47:59 -0400 Subject: [rspec-users] should_receive.again In-Reply-To: <276ff9870706031919g76913dbdm4a0676b1fd714148@mail.gmail.com> References: <9E413F4D-CAB0-445C-9A9C-28B11EAE3343@parkerhill.com> <8d961d900706030321l2d5e1d5an953b06ddb40cd721@mail.gmail.com> <276ff9870706031919g76913dbdm4a0676b1fd714148@mail.gmail.com> Message-ID: <260A5104-9CE8-4A2A-B155-1E20919FB733@parkerhill.com> The issue was the number of calls. I really only want to count the number of calls by the specific action. But there's a before filter which calls the same method. I couldn't stub out the before filter itself because it sets an instance variable used by the action. Does that make sense? So my example has to know more about the implementation than I would like. On Jun 3, 2007, at 10:19 PM, nicholas a. evans wrote: > On 6/3/07, aslak hellesoy wrote: >> On 6/3/07, Jonathan Linowes wrote: >>> This would be helpful because I have a setup method that sets up the >>> should_receive once, and then some specific actions may use that >>> method one or more additional times. >> >> @thing.should_receive(:method).exactly(3).times.and_return(@value) >> http://rspec.rubyforge.org/rdoc/classes/Spec/Mocks.html > > You might also consider simply stubbing the method in your setup, and > only specifying a number of times called in specific examples. > Whenever reasonable, I try to follow the advice given by David > Chelimsky and aim for no mock expectations in the setup and only one > per example: http://blog.davidchelimsky.net/articles/2006/11/09/ > tutorial-rspec-stubs-and-mocks > > Also, you might want to think about whether or not you really should > be locking down your specification to exactly three (or however many) > calls. Without knowing your details I can't say for certain if this > applies to you, but often the exact number of calls is an > implementation detail that your spec doesn't need to worry about. If > that's the case, I might loosen up the mock expectation a little bit > by simply saying > @thing.should_receive(:method).at_least(:once).and_return(@value) > > -- > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From anthony at digitalphenom.com Mon Jun 4 00:10:06 2007 From: anthony at digitalphenom.com (Anthony Carlos) Date: Mon, 4 Jun 2007 00:10:06 -0400 Subject: [rspec-users] Specs for ApplicationController, where to put them? In-Reply-To: <57c63afe0706031255g4f96cf7h1263e4b067167b26@mail.gmail.com> References: <2D69A175-3C2A-4E38-A006-68695DF3E359@wincent.com> <050EF0F7-7EE3-4995-AFE3-0FD735F98167@digitalphenom.com> <583D179D-743C-49BB-9CFA-B31AF11C71E2@digitalphenom.com> <57c63afe0705291652r2bb1ea05o29ed4b4ec96e29ed@mail.gmail.com> <57c63afe0706031255g4f96cf7h1263e4b067167b26@mail.gmail.com> Message-ID: <646A1FE4-73D2-42D4-B853-DEAEA8528D70@digitalphenom.com> David: I could have had a V8?!?! (For the young, that's a reference to the commercial for V8 fruit and vegetable juice where the actor slaps himself in the forehead wishing he drank the delicious and nutritious beverage instead of some other more stupid beverage choice). Thanks for the unbelievably clear explanation. I will comment on your blog. -Anthony On Jun 3, 2007, at 3:55 PM, David Chelimsky wrote: > On 6/1/07, Anthony Carlos wrote: >> >> On May 29, 2007, at 7:52 PM, David Chelimsky wrote: >> >>>> My problem now, is that I get a nil object error. Specifically, in >>>> application.rb, I'm trying to call request.parameters and >>>> request is >>>> nil. It seems that request is not part of the context anymore >>>> since I >>>> moved the behavior out to a separate file. How do I regain >>>> access to >>>> request, response, assigns, flash, and session? >>> >>> The fact that the shared examples are in another file should have no >>> bearing on whether they have runtime access to whatever is in the >>> behaviours that include them. This all sounds odd to me. Can you >>> post >>> a backtrace please? >> >> David: >> >> Here's the output from rake spec:autotest: >> >> : >> QuestionnairesController >> - should flash notice when session[:current_user_id] does not exist >> (ERROR - 1) >> - should return true when session[:current_user_id] exists >> - should respond to current_user >> - should respond to authenticate >> >> 1) >> NoMethodError in 'QuestionnairesController should flash notice when >> session[:current_user_id] does not exist' >> You have a nil object when you didn't expect it! >> The error occurred while evaluating nil.parameters >> /Users/aec/ror/aca/aca_trunk/config/../app/controllers/ >> application.rb: >> 11:in `authenticate' >> ./spec/controllers/application_controller_spec.rb:24: >> >> ===== >> Here's application_controller_spec.rb: >> require File.dirname(__FILE__) + '/../spec_helper' >> >> unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers") >> describe "All controllers", :shared => true do >> it "should respond to authenticate" do >> controller.should respond_to(:authenticate) >> end >> it "should respond to current_user" do >> controller.should respond_to(:current_user) >> end >> it "should return true when session[:current_user_id] exists" do >> session[:current_user_id] = 12345 >> controller.authenticate.should == true >> end >> it "should flash notice when session[:current_user_id] does not >> exist" do >> session[:current_user_id] = nil >> controller.authenticate # <===== This is line 24 where the >> error occurs! >> flash[:notice].should == 'Please login.' >> end >> end >> end >> >> ===== >> Here's application.rb: >> class ApplicationController < ActionController::Base >> # Pick a unique cookie name to distinguish our session data from >> others' >> session :session_key => '_unique_session_id' >> >> def authenticate >> unless session[:current_user_id] >> flash[:notice] = 'Please login.' >> session[:jumpto] = request.parameters # <===== This is line 11 >> where nil.parameters is called! >> redirect_to(:controller => 'ums', :action => 'login') >> end >> true >> end >> >> def current_user >> @current_user = User.find(session[:current_user_id]) >> end >> end >> >> ===== >> Finally, here's questionnaires_controller_spec.rb: >> require File.dirname(__FILE__) + '/../spec_helper' >> require File.dirname(__FILE__) + '/application_controller_spec' >> unless Spec::DSL::Behaviour.find_shared_behaviour("All controllers") >> >> describe QuestionnairesController do >> it_should_behave_like "All controllers" >> end >> : >> >> ===== >> So as far as I can tell, my QuestionnairesController should behave >> like "All controllers" which puts us at >> application_controller_spec.rb. The first 3 examples pass, but the >> fourth, which calls ApplicationController.authenticate, gives me the >> nil object. But, isn't request an object that is always available to >> controllers? >> >> Wait a second... >> >> There's no get or post command here! Maybe thats why request is nil? >> I think so. Never mind! Sorry about the false alarm. I don't want to >> be the boy who cried wolf. >> >> On the other hand, I think I avoided using get or post because this >> is an abstract action that gets called by other concrete classes. So >> how does one test logic in an abstract class? > > This question inspired me to write this blog post: > > http://blog.davidchelimsky.net/articles/2007/06/03/oxymoron-testing- > behaviour-of-abstractions > > Feel free to comment directly on the blog. > > Cheers, > David > >> Smells like a job for a >> mock, of course?!? I'll look over the list archive-- I think other >> people have asked this question before. >> >> Thanks for helping out the slow learners, >> >> -Anthony >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Mon Jun 4 00:36:10 2007 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 3 Jun 2007 23:36:10 -0500 Subject: [rspec-users] Another attempt for a succinct model validation DSL In-Reply-To: <276ff9870706031952n67e5507clb22837e1da1cc3bb@mail.gmail.com> References: <276ff9870705311334t7dd388c8mf79fc6e7fa5aa695@mail.gmail.com> <276ff9870706010811s58a95c54x642296c18837fbee@mail.gmail.com> <167b6aa00706010917h740ca9d4k20d543d7c11da8f8@mail.gmail.com> <276ff9870706011002m37cdd8edof459546602c8a3cd@mail.gmail.com> <167b6aa00706011019n46e3471dy6c0a526de1e31318@mail.gmail.com> <276ff9870706011116o714208f4yc8e7243af7b774bb@mail.gmail.com> <167b6aa00706011147q3e4449e0rcdf5ba06cff7c007@mail.gmail.com> <57c63afe0706011501w6b48892ej654223495d956344@mail.gmail.com> <276ff9870706020732y511e93aahd50b0fad62969fda@mail.gmail.com> <276ff9870706031952n67e5507clb22837e1da1cc3bb@mail.gmail.com> Message-ID: <57c63afe0706032136s1af39b1bq619714f7ef33bbe9@mail.gmail.com> On 6/3/07, nicholas a. evans wrote: > On 6/2/07, nicholas a. evans wrote: > > At any rate, I just put together my first draft implementation > > (developed spec-first). It still doesn't check the error messages > > yet, and it's only handling single fields at the moment, but I'll do > > those after I get home from the market. After that I'll figure out > > how to get it working with my desired "shortcut" syntax. > > So I haven't gotten around to my preferred shortcut syntax yet, but I > did add support to check for error messages and handle multiple > fields, so it should be quite usable now. I took the approach that > when specifying single field validations it only checks for errors on > that field, but when specifying multi-field validations it checks for > any errors on the object. That felt like simplest and least > surprising solution to me. My specs assume that you can use sqlite3 > (to do an in-memory DB). > > http://svn.ekenosen.net/public/rspec_validation_matcher/ > > http://pastie.caboo.se/67509 shows what it currently can do, as well > as my current thoughts on how I would *really* like to be doing this. > I'm eager to hear any comments on it. :-) Personally, I think @person.should accept("Danny", "Elfman") ;) > > -- > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From daniel at danielfischer.com Mon Jun 4 04:16:45 2007 From: daniel at danielfischer.com (Fischer, Daniel) Date: Mon, 4 Jun 2007 01:16:45 -0700 Subject: [rspec-users] I'm really bad at controllers, help please. In-Reply-To: <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> References: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> Message-ID: <7e565b5c0706040116q73948fccr488c89f3a6068122@mail.gmail.com> Jonathan, Thanks a lot - that accomplished what I wanted to do, and provided me with insight. I'm still having some sort of misunderstanding though, because I'm having an error like this now (in a different section though; this time regarding post: 2) Spec::Mocks::MockExpectationError in 'MessagesController handling POST /messages should create a new message' Mock 'Message_1026' received unexpected message :author_id= with (202018) /Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/messages_controller.rb:31:in `create' /Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in `process_without_test' ./spec/controllers/messages_controller_spec.rb:206:in `do_post' ./spec/controllers/messages_controller_spec.rb:211: yet I've tried both Message.stub!(:author_id).and_return(1) and @ message.stub!(:author_id).and_return(1) why would it still fail after stubbing both possibilities? In my controller i'm calling @message.author_id = current_user.id Thanks! On 6/2/07, Jonathan Linowes wrote: > > fyi, i'm finding the should_receive(:messages) to be too brittle > because any time I add a call like @user.messages.find or whatever it > breaks my spec (requiring I do .twice or .times(N) . IMO the spec > shouldn't be so sensitive to implementation of the behavior, so i'm > now stubbing the association > > replaced > @user.should_receive(:messages).and_return(@messages) > with > @user.stub!(:messages).and_return(@messages) > > > > On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote: > > > I'm nearly as new at this as you are I'm I'm just figuring things out > > myself too. > > > > I think your problem is method :messages is being called on an > > instance of User, not the class, so you need > > > > @user.stub!(:messages).and_return([@message]) > > > > here's some clues to how I've gotten similar things working > > To diagnose problems with my examples, I > > - watch the test.log file. You could use 'tail' or on mac use the > > 'console' utility > > - use the -e option in spec to run one example at a time. I copy/ > > paste the test name from the terminal window to avoid a lot of typing > > - sometimes I comment out code in my app methods to limit what its > > actually doing and then add one line at a time to "re-develop" the > > method > > - similarly I may create an extra test method in my controller or > > model to isolate things I dont understand between the spec and the > > code. > > > > I also tend to use should_receive rather than stubs, it provides more > > details (although seems to make the tests more brittle) > > > > thus, > > > > # separate for reuse with multiple describe cases > > def setup_messages do > > @user = mock_model(User) > > @message = mock_model(Message) > > @messages = [@message] > > end > > > > def before(:each) do > > setup_messages > > User.should_receive(:find).with("1").and_return(@user) > > @user.should_receive(:messages).and_return(@messages) > > end > > > > def do_get > > get :index, :user_id => 1 > > end > > > > it "should find all messages" do > > do_get > > assigns[:messages].should == @messages > > end > > > > I think this is right. Please let me know > > :) > > > > > > On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote: > > > >> Hey, > >> > >> Sorry for so many questions - I'm really bad at this right now. > >> > >> I'm trying to cover the following code w/ rspec > >> > >> def index > >> if params[:user_id] > >> @user = User.find(params[:user_id]) > >> @messages = @user.messages > >> end > >> end > >> > >> So basically what I'm doing is listing all the messages for a user, > >> provided there is an id parameter. > >> > >> describe MessagesController, " handling GET /messages for a user" do > >> > >> before do > >> @message = mock_model(Message) > >> @message.stub!(:user_id).and_return(1) > >> @user = mock_model(User) > >> @user.stub!(:id).and_return(1) > >> User.stub!(:messages).and_return([@message]) > >> User.stub!(:find).and_return([@user]) > >> end > >> > >> def do_get > >> get :index, :user_id => 1 > >> end > >> > >> it "should be successful" do > >> do_get > >> response.should be_success > >> end > >> > >> it "should render index template" do > >> do_get > >> response.should render_template('index') > >> end > >> > >> it "should find all messages" do > >> User.should_receive(:messages).and_return([@message]) > >> do_get > >> end > >> > >> it "should assign the found messages for the view" do > >> do_get > >> assigns[:messages].should == [@message] > >> end > >> end > >> > >> > >> I'm trying to use the basic scaffold spec, but I'm absolutely > >> clueless > >> on what the proper way to handle this is, I'm not even sure the > >> proper > >> way I should mock the messages method, so it'll return a "stub?" of a > >> collection, or whatever the proper term is. > >> > >> I'm sorry you have to deal with a newb, but if you could really help > >> me out I'd appreciate it a ton, thanks! > >> -- > >> -Daniel Fischer > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- -Daniel Fischer http://danielfischer.com - Geek Blog http://abigfisch.com - Portfolio http://writersbeat.com - Writing Community -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070604/876c5339/attachment-0001.html From aslak.hellesoy at gmail.com Mon Jun 4 04:58:04 2007 From: aslak.hellesoy at gmail.com (aslak hellesoy) Date: Mon, 4 Jun 2007 10:58:04 +0200 Subject: [rspec-users] I'm really bad at controllers, help please. In-Reply-To: <7e565b5c0706040116q73948fccr488c89f3a6068122@mail.gmail.com> References: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> <7e565b5c0706040116q73948fccr488c89f3a6068122@mail.gmail.com> Message-ID: <8d961d900706040158u46a7e8a4jf8bd80be5f4c609f@mail.gmail.com> On 6/4/07, Fischer, Daniel wrote: > Jonathan, > > Thanks a lot - that accomplished what I wanted to do, and provided me with > insight. > > I'm still having some sort of misunderstanding though, because I'm having an > error like this now (in a different section though; this time regarding > post: > > 2) > Spec::Mocks::MockExpectationError in 'MessagesController > handling POST /messages should create a new message' > Mock 'Message_1026' received unexpected message :author_id= with (202018) > /Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/messages_controller.rb:31:in > `create' > /Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in > `process_without_test' > ./spec/controllers/messages_controller_spec.rb:206:in > `do_post' > ./spec/controllers/messages_controller_spec.rb:211: > > yet I've tried both Message.stub!(:author_id).and_return(1) and > @message.stub!(:author_id).and_return(1) why would it still fail after > stubbing both possibilities? > You're stubbing the accessor (:author_id) instead of the accessor (:author_id=) Aslak > In my controller i'm calling @message.author_id = current_user.id > > Thanks! > > On 6/2/07, Jonathan Linowes wrote: > > fyi, i'm finding the should_receive(:messages) to be too brittle > > because any time I add a call like @ user.messages.find or whatever it > > breaks my spec (requiring I do .twice or .times(N) . IMO the spec > > shouldn't be so sensitive to implementation of the behavior, so i'm > > now stubbing the association > > > > replaced > > > @user.should_receive(:messages).and_return(@messages) > > with > > @user.stub!(:messages).and_return(@messages) > > > > > > > > On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote: > > > > > I'm nearly as new at this as you are I'm I'm just figuring things out > > > myself too. > > > > > > I think your problem is method :messages is being called on an > > > instance of User, not the class, so you need > > > > > > @user.stub!(:messages).and_return([@message]) > > > > > > here's some clues to how I've gotten similar things working > > > To diagnose problems with my examples, I > > > - watch the test.log file. You could use 'tail' or on mac use the > > > 'console' utility > > > - use the -e option in spec to run one example at a time. I copy/ > > > paste the test name from the terminal window to avoid a lot of typing > > > - sometimes I comment out code in my app methods to limit what its > > > actually doing and then add one line at a time to "re-develop" the > > > method > > > - similarly I may create an extra test method in my controller or > > > model to isolate things I dont understand between the spec and the > > > code. > > > > > > I also tend to use should_receive rather than stubs, it provides more > > > details (although seems to make the tests more brittle) > > > > > > thus, > > > > > > # separate for reuse with multiple describe cases > > > def setup_messages do > > > @user = mock_model(User) > > > @message = mock_model(Message) > > > @messages = [@message] > > > end > > > > > > def before(:each) do > > > setup_messages > > > > User.should_receive(:find).with("1").and_return(@user) > > > > @user.should_receive(:messages).and_return(@messages) > > > end > > > > > > def do_get > > > get :index, :user_id => 1 > > > end > > > > > > it "should find all messages" do > > > do_get > > > assigns[:messages].should == @messages > > > end > > > > > > I think this is right. Please let me know > > > :) > > > > > > > > > On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote: > > > > > >> Hey, > > >> > > >> Sorry for so many questions - I'm really bad at this right now. > > >> > > >> I'm trying to cover the following code w/ rspec > > >> > > >> def index > > >> if params[:user_id] > > >> @user = User.find(params[:user_id]) > > >> @messages = @user.messages > > >> end > > >> end > > >> > > >> So basically what I'm doing is listing all the messages for a user, > > >> provided there is an id parameter. > > >> > > >> describe MessagesController, " handling GET /messages for a user" do > > >> > > >> before do > > >> @message = mock_model(Message) > > >> @message.stub!(:user_id).and_return(1) > > >> @user = mock_model(User) > > >> @user.stub!(:id).and_return(1) > > >> User.stub!(:messages).and_return([@message]) > > >> User.stub!(:find).and_return([@user]) > > >> end > > >> > > >> def do_get > > >> get :index, :user_id => 1 > > >> end > > >> > > >> it "should be successful" do > > >> do_get > > >> response.should be_success > > >> end > > >> > > >> it "should render index template" do > > >> do_get > > >> response.should render_template('index') > > >> end > > >> > > >> it "should find all messages" do > > >> > User.should_receive(:messages).and_return([@message]) > > >> do_get > > >> end > > >> > > >> it "should assign the found messages for the view" do > > >> do_get > > >> assigns[:messages].should == [@message] > > >> end > > >> end > > >> > > >> > > >> I'm trying to use the basic scaffold spec, but I'm absolutely > > >> clueless > > >> on what the proper way to handle this is, I'm not even sure the > > >> proper > > >> way I should mock the messages method, so it'll return a "stub?" of a > > >> collection, or whatever the proper term is. > > >> > > >> I'm sorry you have to deal with a newb, but if you could really help > > >> me out I'd appreciate it a ton, thanks! > > >> -- > > >> -Daniel Fischer > > >> _______________________________________________ > > >> rspec-users mailing list > > >> rspec-users at rubyforge.org > > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > -- > -Daniel Fischer > > http://danielfischer.com - Geek Blog > http://abigfisch.com - Portfolio > http://writersbeat.com - Writing Community > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From aslak.hellesoy at gmail.com Mon Jun 4 04:59:01 2007 From: aslak.hellesoy at gmail.com (aslak hellesoy) Date: Mon, 4 Jun 2007 10:59:01 +0200 Subject: [rspec-users] I'm really bad at controllers, help please. In-Reply-To: <8d961d900706040158u46a7e8a4jf8bd80be5f4c609f@mail.gmail.com> References: <7e565b5c0706020127m121fc734o10dc9b6cd763e635@mail.gmail.com> <1BA71784-13D3-4086-9D71-0E46D43D8AE9@parkerhill.com> <581CFB5F-3C4A-4383-9E3E-74D609D1B177@parkerhill.com> <7e565b5c0706040116q73948fccr488c89f3a6068122@mail.gmail.com> <8d961d900706040158u46a7e8a4jf8bd80be5f4c609f@mail.gmail.com> Message-ID: <8d961d900706040159v7ed99935rc81f660f6b5114dd@mail.gmail.com> On 6/4/07, aslak hellesoy wrote: > On 6/4/07, Fischer, Daniel wrote: > > Jonathan, > > > > Thanks a lot - that accomplished what I wanted to do, and provided me with > > insight. > > > > I'm still having some sort of misunderstanding though, because I'm having an > > error like this now (in a different section though; this time regarding > > post: > > > > 2) > > Spec::Mocks::MockExpectationError in 'MessagesController > > handling POST /messages should create a new message' > > Mock 'Message_1026' received unexpected message :author_id= with (202018) > > /Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/messages_controller.rb:31:in > > `create' > > /Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in > > `process_without_test' > > ./spec/controllers/messages_controller_spec.rb:206:in > > `do_post' > > ./spec/controllers/messages_controller_spec.rb:211: > > > > yet I've tried both Message.stub!(:author_id).and_return(1) and > > @message.stub!(:author_id).and_return(1) why would it still fail after > > stubbing both possibilities? > > > > You're stubbing the accessor (:author_id) instead of the accessor (:author_id=) I meant: You're stubbing the accessor (:author_id) instead of the mutator (:author_id=) Accessor == getter, mutator == setter. Aslak > > Aslak > > > In my controller i'm calling @message.author_id = current_user.id > > > > Thanks! > > > > On 6/2/07, Jonathan Linowes wrote: > > > fyi, i'm finding the should_receive(:messages) to be too brittle > > > because any time I add a call like @ user.messages.find or whatever it > > > breaks my spec (requiring I do .twice or .times(N) . IMO the spec > > > shouldn't be so sensitive to implementation of the behavior, so i'm > > > now stubbing the association > > > > > > replaced > > > > > @user.should_receive(:messages).and_return(@messages) > > > with > > > @user.stub!(:messages).and_return(@messages) > > > > > > > > > > > > On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote: > > > > > > > I'm nearly as new at this as you are I'm I'm just figuring things out > > > > myself too. > > > > > > > > I think your problem is method :messages is being called on an > > > > instance of User, not the class, so you need > > > > > > > > @user.stub!(:messages).and_return([@message]) > > > > > > > > here's some clues to how I've gotten similar things working > > > > To diagnose problems with my examples, I > > > > - watch the test.log file. You could use 'tail' or on mac use the > > > > 'console' utility > > > > - use the -e option in spec to run one example at a time. I copy/ > > > > paste the test name from the terminal window to avoid a lot of typing > > > > - sometimes I comment out code in my app methods to limit what its > > > > actually doing and then add one line at a time to "re-develop" the > > > > method > > > > - similarly I may create an extra test method in my controller or > > > > model to isolate things I dont understand between the spec and the > > > > code. > > > > > > > > I also tend to use should_receive rather than stubs, it provides more > > > > details (although seems to make the tests more brittle) > > > > > > > > thus, > > > > > > > > # separate for reuse with multiple describe cases > > > > def setup_messages do > > > > @user = mock_model(User) > > > > @message = mock_model(Message) > > > > @messages = [@message] > > > > end > > > > > > > > def before(:each) do > > > > setup_messages > > > > > > User.should_receive(:find).with("1").and_return(@user) > > > > > > @user.should_receive(:messages).and_return(@messages) > > > > end > > > > > > > > def do_get > > > > get :index, :user_id => 1 > > > > end > > > > > > > > it "should find all messages" do > > > > do_get > > > > assigns[:messages].should == @messages > > > > end > > > > > > > > I think this is right. Please let me know > > > > :) > > > > > > > > > > > > On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote: > > > > > > > >> Hey, > > > >> > > > >> Sorry for so many questions - I'm really bad at this right now. > > > >> > > > >> I'm trying to cover the following code w/ rspec > > > >> > > > >> def index > > > >> if params[:user_id] > > > >> @user = User.find(params[:user_id]) > > > >> @messages = @user.messages > > > >> end > > > >> end > > > >> > > > >> So basically what I'm doing is listing all the messages for a user, > > > >> provided there is an id parameter. > > > >> > > > >> describe MessagesController, " handling GET /messages for a user" do > > > >> > > > >> before do > > > >> @message = mock_model(Message) > > > >> @message.stub!(:user_id).and_return(1) > > > >> @user = mock_model(User) > > > >> @user.stub!(:id).and_return(1) > > > >> User.stub!(:messages).and_return([@message]) > > > >> User.stub!(:find).and_return([@user]) > > > >> end > > > >> > > > >> def do_get > > > >> get :index, :user_id => 1 > > > >> end > > > >> > > > >> it "should be successful" do > > > >> do_get > > > >> response.should be_success > > > >> end > > > >> > > > >> it "should render index template" do > > > >> do_get > > > >> response.should render_template('index') > > > >> end > > > >> > > > >> it "should find all messages" do > > > >> > > User.should_receive(:messages).and_return([@message]) > > > >> do_get > > > >> end > > > >> > > > >> it "should assign the found messages for the view" do > > > >> do_get > > > >> assigns[:messages].should == [@message] > > > >> end > > > >> end > > > >> > > > >> > > > >> I'm trying to use the basic scaffold spec, but I'm absolutely > > > >> clueless > > > >> on what the proper way to handle this is, I'm not even sure the > > > >> proper > > > >> way I should mock the messages method, so it'll return a "stub?" of a > > > >> collection, or whatever the proper term is. > > > >> > > > >> I'm sorry you have to deal with a newb, but if you could really help > > > >> me out I'd appreciate it a ton, thanks! > > > >> -- > > > >> -Daniel Fischer > > > >> _______________________________________________ > > > >> rspec-users mailing list > > > >> rspec-users at rubyforge.org > > > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rsp