On 11.4.2008, at 4.57, newbie wrote:
Thanks for the direction. I'm glad to hear that I'm going in the right direction. I just have a final question and I seem to be hitting a stub/mocking road block at the moment. In my controller I have this... def index @entries = current_user.entries end How would I rspec this out correctly? This is what I have but it looks wrong. before(:each) do @current_user = mock_model(User, :id => 1) @entry = mock_model(Entry, :user_id => 1) controller.stub!(:current_user).and_return(@current_user) controller.stub!(:entries).and_return(@entry) end describe "on the index page" do it "should show all of the current_users entries" do @current_user.should_receive(:entries).with(@entry).and_return(true)
This is wrong. The with method means parameters the expected method call receives. In your case it gets no parameters. Besides, since you stub the entries method anyway, you can just specify the behaviour.
What you probably want with the code above is this: assigns[:entries].should == @entry //jarkko
end end On Apr 7, 11:31 am, "Bryan Ray" <[EMAIL PROTECTED]> wrote:Responses shown below...Sorry this isn't extremely informative ... in a bit of a rush. Hopefully itwill point you in the appropriate direction. On Sun, Apr 6, 2008 at 2:17 PM, newbie <[EMAIL PROTECTED]> wrote:I'm new to Rspec and I'm having a bit of trouble with this controller that I'm testing. I think I'm doing it right but I guess my syntax iswrong. I'm currently using the "acts_as_authenticated" plug in.What I want to test out isEventController on entering the tickets_page - should show the tickets_page if the current_user has NOT entered this page today----------------------------------------------------- Below is my event controller -----------------------------------------------------class EventController < ApplicationControllerbefore_filter :login_requireddef tickets_page if current_user.has_already_entered_today? flash[:notice] = 'Come back tomorrow' redirect_to :action => 'home' else flash[:notice] = 'Welcome' end end end----------------------------------------------------- Below is my rspec for this controller -----------------------------------------------------require File.dirname(__FILE__) + '/../spec_helper' describe EventController dobefore(:each) do @current_user = mock_model(User, :id => 1) controller.stub!(:current_user).and_return(@current_user) controller.stub!(:login_required).and_return(:true) endfixtures :usersdescribe "on entering the tickets page" do it "should show the tickets_page if the current_user has NOT entered this page today" do controller.current_user.stub! (:has_already_entered_today?).and_return(:false) get :tickets_pagecontroller .current_user.should_receive(:has_already_entered_today?).with(:f alse).and_return(:false)response.should render_template(:tickets_page) end endend----------------------------------------------------- My errors ----------------------------------------------------- Mock 'User_1001' received unexpected message :has_already_entered_today? with (no args)So this is a good start ... it means that the controller action is actually being mocked out ... you just need to keep moving forward mocking/ stubbing out the rest of your controller actions. it's complaining that it doesn't know what to do on your *current_user.has_already_entered_today?* method.@current_user.stub!(:has_already_entered_today?).and_return(<insert true orfalse here>)Anyone can help with any direction of what I might be doing wrong?_______________________________________________ rspec-users mailing list [EMAIL PROTECTED] http://rubyforge.org/mailman/listinfo/rspec-users-- Bryan Rayhttp://www.bryanray.net"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to producebigger and better idiots. So far, the Universe is winning." _______________________________________________ rspec-users mailing list [EMAIL PROTECTED]://rubyforge.org/mailman/listinfo/rspec-users_______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
-- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users