I have dabbled with RSpec with the Beta books but am only now getting my teeth in to it. Because of this and because I have no one to ask/bounce ideas off, I am asking for a kind soul to look at what I''ve done for my first example. Am I on the right track with the way I''m thinking? My first attempt at testing my real live thing was the following (comments included to help) describe "GET edit" do context "the product cutoff time has passed" do it "prevents editing of the product" do ## ProductType object must be received on an edit ProductType.should_receive(:find).with("1").and_return(ProductType) ProductType.should_receive(:products).and_return(Product) Product.stub(:find).with("1").and_return(Product) ## this is the thing I really want to test Product.should_receive(:product_cutoff_passed?).and_return(true) ## before filters controller.stub(:authorise_is_admin).and_return(true) controller.stub(:require_user).and_return(user) get :edit, :product_type_id => 1, :id => 1 flash[:notice].should == "The product is closed for editing" response.should redirect_to(products_path) end end end I got the above example passing and then went about refactoring it and this is what I have now def require_user controller.stub(:require_user).and_return(User) end def authorise_is_admin controller.stub(:authorise_is_admin).and_return(true) end describe ProductsController do ## this is required for all actions in this controller before(:each) do require_user end let(:product_type) { mock_model(ProductType).as_null_object } let(:product) { mock_model(Product).as_null_object } describe "GET edit" do ## this is required for this action before(:each) do authorise_is_admin end ## this now has its own test as it is fundamental to the action it "creates a ProductType object" do ## message expectation ProductType.should_receive(:find).with("1").and_return(product_type)45 get :edit, :product_type_id => 1, :id => 1 end ## the message expectations in my first example are now stubs in this example ## as they are not the main focus of this test context "the product cutoff time has passed" do it "prevents editing of the product" do ProductType.stub(:find).with("1").and_return(product_type) ## would like to use stub_chain but one of the stubs receives arguments product_type.stub(:products).and_return(product) product.stub(:find).with("1").and_return(product) ## message expectation product.should_receive(:product_cutoff_passed?).and_return(true) get :edit, :product_type_id => 1, :id => 1 flash[:notice].should == "The product is closed for editing" response.should redirect_to(products_path) end end end end I hope you don;t mind if I do this as I will need your feedback to get me in the right way of thinking. Thank you -ants -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110106/93e86043/attachment.html>
On 6 January 2011 20:39, Ants Pants <antsmailinglist at gmail.com> wrote:> I have dabbled with RSpec with the Beta books but am only now getting my > teeth in to it. Because of this and because I have no one to ask/bounce > ideas off, I am asking for a kind soul to look at what I''ve done for my > first example. Am I on the right track with the way I''m thinking? > > My first attempt at testing my real live thing was the following (comments > included to help) > > describe "GET edit" do > context "the product cutoff time has passed" do > it "prevents editing of the product" do > ## ProductType object must be received on an edit > ProductType.should_receive(:find).with("1").and_return(ProductType) > ProductType.should_receive(:products).and_return(Product) > > Product.stub(:find).with("1").and_return(Product) > > ## this is the thing I really want to test > Product.should_receive(:product_cutoff_passed?).and_return(true) > > ## before filters > controller.stub(:authorise_is_admin).and_return(true) > controller.stub(:require_user).and_return(user) > > get :edit, :product_type_id => 1, :id => 1 > > flash[:notice].should == "The product is closed for editing" > response.should redirect_to(products_path) > > end > end > end > > > I got the above example passing and then went about refactoring it and this > is what I have now > > > def require_user > controller.stub(:require_user).and_return(User) > end > > def authorise_is_admin > controller.stub(:authorise_is_admin).and_return(true) > end > > describe ProductsController do > ## this is required for all actions in this controller > before(:each) do > require_user > end > > let(:product_type) { mock_model(ProductType).as_null_object } > let(:product) { mock_model(Product).as_null_object } > > describe "GET edit" do > ## this is required for this action > before(:each) do > authorise_is_admin > end > > ## this now has its own test as it is fundamental to the action > it "creates a ProductType object" do > ## message expectation > > ProductType.should_receive(:find).with("1").and_return(product_type)45 > get :edit, :product_type_id => 1, :id => 1 > end > > ## the message expectations in my first example are now stubs in this > example > ## as they are not the main focus of this test > context "the product cutoff time has passed" do > it "prevents editing of the product" do > ProductType.stub(:find).with("1").and_return(product_type) > > ## would like to use stub_chain but one of the stubs receives > arguments > product_type.stub(:products).and_return(product) > product.stub(:find).with("1").and_return(product) > > ## message expectation > product.should_receive(:product_cutoff_passed?).and_return(true) > > get :edit, :product_type_id => 1, :id => 1 > > flash[:notice].should == "The product is closed for editing" > response.should redirect_to(products_path) > > end > end > end > end > > I hope you don;t mind if I do this as I will need your feedback to get me > in the right way of thinking. > > Thank you > > -ants >If you are getting started with RSpec don''t test Rails Controllers - its the hardest thing to do with RSpec, and probably the most pointless. Instead Google for Jamis Buck''s fat model skinny controller blog post, make your controllers do as little as possible and put all logic in the model. Then use rspec to test the public methods in your model. This will give you a much shallower learning curve, and be much more productive. When you''ve done a fair amount of rspec model testing, and you''re feeling a need to test more of the stack, have a look at Cucumber. Don''t forget the RSpec book. Do codebreaker line by line, typing in everything (no cut and paste), and you will learn lots. HTH Andrew> _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110121/2f83d256/attachment.html>
Also, try to keep have just one expectation (IE: call to #should) in each test. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110124/019dde2a/attachment.html>
Yes, I''ve come a long way since posting that. I''m embarrassed by that post but after reading the book and getting started, I just wanted some feedback. I''m a proffie now!! On 24 January 2011 21:36, Nick <nick at deadorange.com> wrote:> Also, try to keep have just one expectation (IE: call to #should) in each > test. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110128/cf6bd559/attachment.html>
On Friday, January 28, 2011 12:09:38 PM UTC-5, ants wrote:> > Yes, I''ve come a long way since posting that. I''m embarrassed by that post > but after reading the book and getting started, I just wanted some > feedback.Hah, don''t be embarassed. Everyone starts out pretty rough with BDD. What''s important is that you improve! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110128/0c82222e/attachment.html>