Andrew WC Brown
2007-Aug-17 19:14 UTC
[rspec-users] [rspec] looking for a simple refresher
I''ve been off the rspec for a few months and I''m trying to get
back on it.
1)
Spec::Mocks::MockExpectationError in ''TicketsController handling POST
/tickets should create a new ticket''
Mock ''Ticket_1001'' expected :new with ({}) once, but received
it 0 times
./spec/controllers/tickets_controller_spec.rb:16:
script/spec:4:
class TicketsController < ApplicationController
def new
Ticket.new
end
end
describe TicketsController, "handling POST /tickets" do
before do
@ticket = mock_model(Ticket, :to_param => ''1'', :save
=> true)
Ticket.stub!(:new).and_return(@ticket)
@params = {}
end
def do_post
post :create, :ticket => @params
end
it "should create a new ticket" do
@ticket.should_receive(:new).with(@params).and_return(@ticket)
do_post
end
end
Would someone provide with an explanation what I have to do to make this
spec pass?
Peepcode hasn''t released their screen cast on rspecing controllers yet
=''(
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20070817/61e34fc1/attachment.html
David Chelimsky
2007-Aug-17 19:17 UTC
[rspec-users] [rspec] looking for a simple refresher
On 8/17/07, Andrew WC Brown <omen.king at gmail.com> wrote:> I''ve been off the rspec for a few months and I''m trying to get back on it. > > 1) > Spec::Mocks::MockExpectationError in ''TicketsController > handling POST /tickets should create a new ticket'' > Mock ''Ticket_1001'' expected :new with ({}) once, but received it 0 times > ./spec/controllers/tickets_controller_spec.rb:16: > script/spec:4: > > class TicketsController < ApplicationController > > def new > Ticket.new > end > > end > > describe TicketsController, "handling POST /tickets" do > > before do > @ticket = mock_model(Ticket, :to_param => ''1'', :save => true) > Ticket.stub!(:new).and_return(@ticket) > @params = {} > end > > def do_post > post :create, :ticket => @params > end > > it "should create a new ticket" do > > @ticket.should_receive(:new).with(@params).and_return(@ticket)This is telling the ticket object to expect new, but it''s the Ticket class that will receive it: Ticket.should_receive(:new).with(@params).and_return(@ticket) That should do it.> do_post > end > > end > > Would someone provide with an explanation what I have to do to make this > spec pass? > Peepcode hasn''t released their screen cast on rspecing controllers yet =''( > > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Andrew WC Brown
2007-Aug-17 23:11 UTC
[rspec-users] [rspec] looking for a simple refresher
I replaced:
@ticket.should_receive(:new).with(@params).and_return(@ticket)
with
Ticket.should_receive(:new).with(@params).and_return(@ticket)
Similar Error
1)
Spec::Mocks::MockExpectationError in ''TicketsController handling POST
/tickets should create a new ticket''
Mock ''Class'' expected :new with ({}) once, but received it 0
times
./spec/controllers/tickets_controller_spec.rb:16:
If I omit @params = {} then I get the error as following
1)
Spec::Mocks::MockExpectationError in ''TicketsController handling POST
/tickets should create a new ticket''
Mock ''Class'' expected :new with (any args) once, but received
it 0 times
./spec/controllers/tickets_controller_spec.rb:16:
So its not receiving the new method at all even though I clearly called it.
Could it be possible that the issue is with the actual Ticket model itself?
On 8/17/07, David Chelimsky <dchelimsky at gmail.com>
wrote:>
> On 8/17/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> > I''ve been off the rspec for a few months and I''m
trying to get back on
> it.
> >
> > 1)
> > Spec::Mocks::MockExpectationError in ''TicketsController
> > handling POST /tickets should create a new ticket''
> > Mock ''Ticket_1001'' expected :new with ({}) once,
but received it 0
> times
> > ./spec/controllers/tickets_controller_spec.rb:16:
> > script/spec:4:
> >
> > class TicketsController < ApplicationController
> >
> > def new
> > Ticket.new
> > end
> >
> > end
> >
> > describe TicketsController, "handling POST /tickets" do
> >
> > before do
> > @ticket = mock_model(Ticket, :to_param =>
''1'', :save => true)
> > Ticket.stub!(:new).and_return(@ticket)
> > @params = {}
> > end
> >
> > def do_post
> > post :create, :ticket => @params
> > end
> >
> > it "should create a new ticket" do
> >
> > @ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> This is telling the ticket object to expect new, but it''s the
Ticket
> class that will receive it:
>
> Ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> That should do it.
>
> > do_post
> > end
> >
> > end
> >
> > Would someone provide with an explanation what I have to do to make
this
> > spec pass?
> > Peepcode hasn''t released their screen cast on rspecing
controllers yet
> =''(
> >
> >
> >
> >
> >
> > _______________________________________________
> > 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
>
--
Monsterbox Productions
putting small businesses on-line
1319 Victoria Avenue East
Thunder Bay, Ontario P7C 1C3
Canada
Andrew WC Brown
web-developer and owner
andrew at monsterboxpro.com
P: 807-626-9009
F: 807-624-2705
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20070817/61e06183/attachment.html
David Chelimsky
2007-Aug-17 23:23 UTC
[rspec-users] [rspec] looking for a simple refresher
Sorry - totally missed the controller code in your first example. You''re posting to create, not new: de create Ticket.new end That should do the trick. On 8/17/07, Andrew WC Brown <omen.king at gmail.com> wrote:> I replaced: > > @ticket.should_receive(:new).with(@params).and_return(@ticket) > > with > > Ticket.should_receive(:new).with(@params).and_return(@ticket) > > Similar Error > > 1) > Spec::Mocks::MockExpectationError in ''TicketsController > handling POST /tickets should create a new ticket'' > Mock ''Class'' expected :new with ({}) once, but received it 0 times > ./spec/controllers/tickets_controller_spec.rb:16: > > If I omit @params = {} then I get the error as following > > 1) > Spec::Mocks::MockExpectationError in ''TicketsController > handling POST /tickets should create a new ticket'' > Mock ''Class'' expected :new with (any args) once, but received it 0 times > ./spec/controllers/tickets_controller_spec.rb:16: > > So its not receiving the new method at all even though I clearly called it. > Could it be possible that the issue is with the actual Ticket model itself? > > > On 8/17/07, David Chelimsky <dchelimsky at gmail.com > wrote: > > On 8/17/07, Andrew WC Brown <omen.king at gmail.com > wrote: > > > I''ve been off the rspec for a few months and I''m trying to get back on > it. > > > > > > 1) > > > Spec::Mocks::MockExpectationError in > ''TicketsController > > > handling POST /tickets should create a new ticket'' > > > Mock ''Ticket_1001'' expected :new with ({}) once, but received it 0 > times > > > ./spec/controllers/tickets_controller_spec.rb:16: > > > script/spec:4: > > > > > > class TicketsController < ApplicationController > > > > > > def new > > > Ticket.new > > > end > > > > > > end > > > > > > describe TicketsController, "handling POST /tickets" do > > > > > > before do > > > @ticket = mock_model(Ticket, :to_param => ''1'', :save => true) > > > Ticket.stub!(:new).and_return(@ticket) > > > @params = {} > > > end > > > > > > def do_post > > > post :create, :ticket => @params > > > end > > > > > > it "should create a new ticket" do > > > > > > > @ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > This is telling the ticket object to expect new, but it''s the Ticket > > class that will receive it: > > > > > Ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > That should do it. > > > > > do_post > > > end > > > > > > end > > > > > > Would someone provide with an explanation what I have to do to make this > > > spec pass? > > > Peepcode hasn''t released their screen cast on rspecing controllers yet > =''( > > > > > > > > > > > > > > > > > > _______________________________________________ > > > 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 > > > > > > -- > Monsterbox Productions > putting small businesses on-line > > 1319 Victoria Avenue East > Thunder Bay, Ontario P7C 1C3 > Canada > > Andrew WC Brown > web-developer and owner > andrew at monsterboxpro.com > P: 807-626-9009 > F: 807-624-2705 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2007-Aug-17 23:24 UTC
[rspec-users] [rspec] looking for a simple refresher
def create (not de create) On 8/17/07, David Chelimsky <dchelimsky at gmail.com> wrote:> Sorry - totally missed the controller code in your first example. > You''re posting to create, not new: > de create > Ticket.new > end > > That should do the trick. > > > On 8/17/07, Andrew WC Brown <omen.king at gmail.com> wrote: > > I replaced: > > > > @ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > with > > > > Ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > Similar Error > > > > 1) > > Spec::Mocks::MockExpectationError in ''TicketsController > > handling POST /tickets should create a new ticket'' > > Mock ''Class'' expected :new with ({}) once, but received it 0 times > > ./spec/controllers/tickets_controller_spec.rb:16: > > > > If I omit @params = {} then I get the error as following > > > > 1) > > Spec::Mocks::MockExpectationError in ''TicketsController > > handling POST /tickets should create a new ticket'' > > Mock ''Class'' expected :new with (any args) once, but received it 0 times > > ./spec/controllers/tickets_controller_spec.rb:16: > > > > So its not receiving the new method at all even though I clearly called it. > > Could it be possible that the issue is with the actual Ticket model itself? > > > > > > On 8/17/07, David Chelimsky <dchelimsky at gmail.com > wrote: > > > On 8/17/07, Andrew WC Brown <omen.king at gmail.com > wrote: > > > > I''ve been off the rspec for a few months and I''m trying to get back on > > it. > > > > > > > > 1) > > > > Spec::Mocks::MockExpectationError in > > ''TicketsController > > > > handling POST /tickets should create a new ticket'' > > > > Mock ''Ticket_1001'' expected :new with ({}) once, but received it 0 > > times > > > > ./spec/controllers/tickets_controller_spec.rb:16: > > > > script/spec:4: > > > > > > > > class TicketsController < ApplicationController > > > > > > > > def new > > > > Ticket.new > > > > end > > > > > > > > end > > > > > > > > describe TicketsController, "handling POST /tickets" do > > > > > > > > before do > > > > @ticket = mock_model(Ticket, :to_param => ''1'', :save => true) > > > > Ticket.stub!(:new).and_return(@ticket) > > > > @params = {} > > > > end > > > > > > > > def do_post > > > > post :create, :ticket => @params > > > > end > > > > > > > > it "should create a new ticket" do > > > > > > > > > > @ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > > > This is telling the ticket object to expect new, but it''s the Ticket > > > class that will receive it: > > > > > > > > Ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > > > That should do it. > > > > > > > do_post > > > > end > > > > > > > > end > > > > > > > > Would someone provide with an explanation what I have to do to make this > > > > spec pass? > > > > Peepcode hasn''t released their screen cast on rspecing controllers yet > > =''( > > > > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > 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 > > > > > > > > > > > -- > > Monsterbox Productions > > putting small businesses on-line > > > > 1319 Victoria Avenue East > > Thunder Bay, Ontario P7C 1C3 > > Canada > > > > Andrew WC Brown > > web-developer and owner > > andrew at monsterboxpro.com > > P: 807-626-9009 > > F: 807-624-2705 > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > >
what''s in the create method of the controller?? you''ve only
given the
contents of the new method - and without knowing what you want it to
do it''s hard to know what will work.
Assuming it''s the normal
def create
@ticket = Ticket.new(params(:ticket])
if @ticket.save
....etc etc
end
then something like the following should work:
describe TicketsController, "handling POST /tickets" do
before do
@ticket = mock_model(Ticket, :save => true)
Ticket.stub!(:new).and_return(@ticket)
@params = {}
end
def do_post
post :create, :ticket => @params
end
it "should create a new ticket" do
ticket.should_receive(:new).with(@params).and_return(@ticket)
do_post
end
it "should assign the ticket for the view" do
do_post
assigns[:ticket].should equal(@ticket)
end
it "should save the new ticket" do
@ticket.should_receive(:save).with().and_return(true)
do_post
end
end
On 18 Aug 2007, at 00:11, Andrew WC Brown wrote:
> I replaced:
>
> @ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> with
>
> Ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> Similar Error
>
> 1)
> Spec::Mocks::MockExpectationError in ''TicketsController handling
> POST /tickets should create a new ticket''
> Mock ''Class'' expected :new with ({}) once, but received
it 0 times
> ./spec/controllers/tickets_controller_spec.rb:16:
>
> If I omit @params = {} then I get the error as following
>
> 1)
> Spec::Mocks::MockExpectationError in ''TicketsController handling
> POST /tickets should create a new ticket''
> Mock ''Class'' expected :new with (any args) once, but
received it 0
> times
> ./spec/controllers/tickets_controller_spec.rb:16:
>
> So its not receiving the new method at all even though I clearly
> called it.
> Could it be possible that the issue is with the actual Ticket model
> itself?
>
> On 8/17/07, David Chelimsky <dchelimsky at gmail.com > wrote:
> On 8/17/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> > I''ve been off the rspec for a few months and I''m
trying to get
> back on it.
> >
> > 1)
> > Spec::Mocks::MockExpectationError in ''TicketsController
> > handling POST /tickets should create a new ticket''
> > Mock ''Ticket_1001'' expected :new with ({}) once,
but received it
> 0 times
> > ./spec/controllers/tickets_controller_spec.rb:16:
> > script/spec:4:
> >
> > class TicketsController < ApplicationController
> >
> > def new
> > Ticket.new
> > end
> >
> > end
> >
> > describe TicketsController, "handling POST /tickets" do
> >
> > before do
> > @ticket = mock_model(Ticket, :to_param =>
''1'', :save => true)
> > Ticket.stub!(:new).and_return(@ticket)
> > @params = {}
> > end
> >
> > def do_post
> > post :create, :ticket => @params
> > end
> >
> > it "should create a new ticket" do
> >
> > @ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> This is telling the ticket object to expect new, but it''s the
Ticket
> class that will receive it:
>
> Ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> That should do it.
>
> > do_post
> > end
> >
> > end
> >
> > Would someone provide with an explanation what I have to do to
> make this
> > spec pass?
> > Peepcode hasn''t released their screen cast on rspecing
> controllers yet =''(
> >
> >
> >
> >
> >
> > _______________________________________________
> > 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
>
>
>
> --
> Monsterbox Productions
> putting small businesses on-line
>
> 1319 Victoria Avenue East
> Thunder Bay, Ontario P7C 1C3
> Canada
>
> Andrew WC Brown
> web-developer and owner
> andrew at monsterboxpro.com
> P: 807-626-9009
> F: 807-624-2705
> _______________________________________________
> 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/20070818/204c9f2c/attachment.html
Andrew WC Brown
2007-Aug-17 23:36 UTC
[rspec-users] [rspec] looking for a simple refresher
That makes a-lot of sense. That ''new'' word threw me off.
My create method is slightly more complex and I''m not sure how to spec
it.
def create
@user = User.find(params[:user_id])
@ticket = @user.tickets.build(params[:ticket])
if @ticket.save
redirect_to tickets_path
else
render new_usr_ticket_path(params[:user_id])
end
end
Would I need two mock_models?
On 8/17/07, rupert <rupert_apsc at rupespad.com>
wrote:>
> what''s in the create method of the controller?? you''ve
only given the
> contents of the new method - and without knowing what you want it to do
it''s
> hard to know what will work.
> Assuming it''s the normal
>
> def create
> @ticket = Ticket.new(params(:ticket])
> if @ticket.save
> ....etc etc
> end
>
> then something like the following should work:
>
> describe TicketsController, "handling POST /tickets" do
> before do
> @ticket = mock_model(Ticket, :save => true)
> Ticket.stub!(:new).and_return(@ticket)
> @params = {}
> end
>
> def do_post
> post :create, :ticket => @params
> end
>
> it "should create a new ticket" do
> ticket.should_receive(:new).with(@params).and_return(@ticket)
> do_post
> end
>
> it "should assign the ticket for the view" do
> do_post
> assigns[:ticket].should equal(@ticket)
> end
>
> it "should save the new ticket" do
> @ticket.should_receive(:save).with().and_return(true)
> do_post
> end
> end
>
> On 18 Aug 2007, at 00:11, Andrew WC Brown wrote:
>
> I replaced:
>
> @ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> with
>
> Ticket.should_receive(:new).with(@params).and_return(@ticket)
>
> Similar Error
>
> 1)
> Spec::Mocks::MockExpectationError in ''TicketsController handling
POST
> /tickets should create a new ticket''
> Mock ''Class'' expected :new with ({}) once, but received
it 0 times
> ./spec/controllers/tickets_controller_spec.rb:16:
>
> If I omit @params = {} then I get the error as following
>
> 1)
> Spec::Mocks::MockExpectationError in ''TicketsController handling
POST
> /tickets should create a new ticket''
> Mock ''Class'' expected :new with (any args) once, but
received it 0 times
> ./spec/controllers/tickets_controller_spec.rb:16:
>
> So its not receiving the new method at all even though I clearly called
> it.
> Could it be possible that the issue is with the actual Ticket model
> itself?
>
> On 8/17/07, David Chelimsky <dchelimsky at gmail.com > wrote:
> >
> > On 8/17/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> > > I''ve been off the rspec for a few months and
I''m trying to get back on
> > it.
> > >
> > > 1)
> > > Spec::Mocks::MockExpectationError in ''TicketsController
> > > handling POST /tickets should create a new ticket''
> > > Mock ''Ticket_1001'' expected :new with ({})
once, but received it 0
> > times
> > > ./spec/controllers/tickets_controller_spec.rb:16:
> > > script/spec:4:
> > >
> > > class TicketsController < ApplicationController
> > >
> > > def new
> > > Ticket.new
> > > end
> > >
> > > end
> > >
> > > describe TicketsController, "handling POST /tickets" do
> > >
> > > before do
> > > @ticket = mock_model(Ticket, :to_param =>
''1'', :save => true)
> > > Ticket.stub!(:new).and_return(@ticket)
> > > @params = {}
> > > end
> > >
> > > def do_post
> > > post :create, :ticket => @params
> > > end
> > >
> > > it "should create a new ticket" do
> > >
> > > @ticket.should_receive(:new).with(@params).and_return(@ticket)
> >
> > This is telling the ticket object to expect new, but it''s the
Ticket
> > class that will receive it:
> >
> > Ticket.should_receive(:new).with(@params).and_return(@ticket)
> >
> > That should do it.
> >
> > > do_post
> > > end
> > >
> > > end
> > >
> > > Would someone provide with an explanation what I have to do to
make
> > this
> > > spec pass?
> > > Peepcode hasn''t released their screen cast on rspecing
controllers yet
> > =''(
> > >
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > 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
> >
>
>
>
> --
> Monsterbox Productions
> putting small businesses on-line
>
> 1319 Victoria Avenue East
> Thunder Bay, Ontario P7C 1C3
> Canada
>
> Andrew WC Brown
> web-developer and owner
> andrew at monsterboxpro.com
> P: 807-626-9009
> F: 807-624-2705_______________________________________________
> 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
>
--
Monsterbox Productions
putting small businesses on-line
1319 Victoria Avenue East
Thunder Bay, Ontario P7C 1C3
Canada
Andrew WC Brown
web-developer and owner
andrew at monsterboxpro.com
P: 807-626-9009
F: 807-624-2705
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20070817/2dad90e6/attachment.html
Not entirely sure how to go about mocking the association, but it''s
going to be something along these lines (totally untested!)....
describe TicketsController, "handling POST /tickets" do
before do
@ticket = mock_model(Ticket, :save => true)
@user_tickets_association = mock("accociation")
@user_tickets_association.stub!(:build).and_return(@ticket)
@user = mock_model(User)
@user.stub!(:tickets).and_return(@user_tickets_association)
@ticket_params = {}
end
def do_post
post :create, :ticket => @params, :user_id => "user_id"
end
it "should find the user" do
User.should_receive(:find).with("user_id").and_return(@user)
do_post
end
it "should assign the user for the view" do
do_post
assigns[:user].should equal(@user)
end
it "should build a new ticket on the user" do
@user_tickets_association.should_receive(:build).with
(@params).and_return(@ticket)
do_post
end
it "should assign the ticket for the view" do
do_post
assigns[:ticket].should equal(@ticket)
end
it "should save the new ticket" do
@ticket.should_receive(:save).with().and_return(true)
do_post
end
it "should save the new ticket" do
@ticket.should_receive(:save).with().and_return(true)
do_post
end
it "should redirect to the ticket path if successful" do
do_post
response.should redirect_to(''/tickets'')
end
it "should render the new page if create fails" do
@ticket.stub!(:save).and_return(false)
do_post
response.should render_template(''new'')
end
end
On 18 Aug 2007, at 00:36, Andrew WC Brown wrote:
> That makes a-lot of sense. That ''new'' word threw me off.
>
> My create method is slightly more complex and I''m not sure how to
> spec it.
>
> def create
> @user = User.find(params[:user_id])
> @ticket = @ user.tickets.build(params[:ticket])
> if @ticket.save
> redirect_to tickets_path
> else
> render new_usr_ticket_path(params[:user_id])
> end
> end
>
>
> Would I need two mock_models?
>
> On 8/17/07, rupert <rupert_apsc at rupespad.com> wrote:
> what''s in the create method of the controller?? you''ve
only given
> the contents of the new method - and without knowing what you want
> it to do it''s hard to know what will work.
>
> Assuming it''s the normal
>
> def create
> @ticket = Ticket.new(params(:ticket])
> if @ticket.save
> ....etc etc
> end
>
> then something like the following should work:
>
> describe TicketsController, "handling POST /tickets" do
> before do
> @ticket = mock_model(Ticket, :save => true)
> Ticket.stub!(:new).and_return(@ticket)
> @params = {}
> end
>
> def do_post
> post :create, :ticket => @params
> end
>
> it "should create a new ticket" do
> ticket.should_receive(:new).with(@params).and_return(@ticket)
> do_post
> end
>
> it "should assign the ticket for the view" do
> do_post
> assigns[:ticket].should equal(@ticket)
> end
>
> it "should save the new ticket" do
> @ticket.should_receive(:save).with().and_return(true)
> do_post
> end
> end
>
>
> On 18 Aug 2007, at 00:11, Andrew WC Brown wrote:
>
>> I replaced:
>>
>> @ticket.should_receive (:new).with(@params).and_return(@ticket)
>>
>> with
>>
>> Ticket.should_receive(:new).with(@params).and_return(@ticket)
>>
>> Similar Error
>>
>> 1)
>> Spec::Mocks::MockExpectationError in ''TicketsController
handling
>> POST /tickets should create a new ticket''
>> Mock ''Class'' expected :new with ({}) once, but
received it 0 times
>> ./spec/controllers/tickets_controller_spec.rb:16:
>>
>> If I omit @params = {} then I get the error as following
>>
>> 1)
>> Spec::Mocks::MockExpectationError in ''TicketsController
handling
>> POST /tickets should create a new ticket''
>> Mock ''Class'' expected :new with (any args) once, but
received it 0
>> times
>> ./spec/controllers/tickets_controller_spec.rb:16:
>>
>> So its not receiving the new method at all even though I clearly
>> called it.
>> Could it be possible that the issue is with the actual Ticket
>> model itself?
>>
>> On 8/17/07, David Chelimsky < dchelimsky at gmail.com > wrote:
>> On 8/17/07, Andrew WC Brown < omen.king at gmail.com > wrote:
>> > I''ve been off the rspec for a few months and I''m
trying to get
>> back on it.
>> >
>> > 1)
>> > Spec::Mocks::MockExpectationError in ''TicketsController
>> > handling POST /tickets should create a new ticket''
>> > Mock ''Ticket_1001'' expected :new with ({})
once, but received
>> it 0 times
>> > ./spec/controllers/tickets_controller_spec.rb:16:
>> > script/spec:4:
>> >
>> > class TicketsController < ApplicationController
>> >
>> > def new
>> > Ticket.new
>> > end
>> >
>> > end
>> >
>> > describe TicketsController, "handling POST /tickets" do
>> >
>> > before do
>> > @ticket = mock_model(Ticket, :to_param =>
''1'', :save => true)
>> > Ticket.stub!(:new).and_return(@ticket)
>> > @params = {}
>> > end
>> >
>> > def do_post
>> > post :create, :ticket => @params
>> > end
>> >
>> > it "should create a new ticket" do
>> >
>> > @ticket.should_receive(:new).with(@params).and_return(@ticket)
>>
>> This is telling the ticket object to expect new, but it''s the
Ticket
>> class that will receive it:
>>
>> Ticket.should_receive(:new).with(@params).and_return(@ticket)
>>
>> That should do it.
>>
>> > do_post
>> > end
>> >
>> > end
>> >
>> > Would someone provide with an explanation what I have to do to
>> make this
>> > spec pass?
>> > Peepcode hasn''t released their screen cast on rspecing
>> controllers yet =''(
>> >
>> >
>> >
>> >
>> >
>> > _______________________________________________
>> > 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
>>
>>
>>
>> --
>> Monsterbox Productions
>> putting small businesses on-line
>>
>> 1319 Victoria Avenue East
>> Thunder Bay, Ontario P7C 1C3
>> Canada
>>
>> Andrew WC Brown
>> web-developer and owner
>> andrew at monsterboxpro.com
>> P: 807-626-9009
>> F: 807-624-2705
>> _______________________________________________
>> 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
>
>
>
> --
> Monsterbox Productions
> putting small businesses on-line
>
> 1319 Victoria Avenue East
> Thunder Bay, Ontario P7C 1C3
> Canada
>
> Andrew WC Brown
> web-developer and owner
> andrew at monsterboxpro.com
> P: 807-626-9009
> F: 807-624-2705
> _______________________________________________
> 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/20070818/ba347153/attachment-0001.html
Andrew WC Brown
2007-Aug-18 00:03 UTC
[rspec-users] [rspec] looking for a simple refresher
thats pretty much what I thought, I''ll give it a push around and e-mail back the results On 8/17/07, rupert <rupert_apsc at rupespad.com> wrote:> > Not entirely sure how to go about mocking the association, but it''s going > to be something along these lines (totally untested!).... > > describe TicketsController, "handling POST /tickets" do > before do > @ticket = mock_model(Ticket, :save => true) > > @user_tickets_association = mock("accociation") > @user_tickets_association.stub!(:build).and_return(@ticket) > > @user = mock_model(User) > @user.stub!(:tickets).and_return(@user_tickets_association) > > @ticket_params = {} > end > def do_post > post :create, :ticket => @params, :user_id => "user_id" > end > > it "should find the user" do > User.should_receive(:find).with("user_id").and_return(@user) > do_post > end > it "should assign the user for the view" do > do_post > assigns[:user].should equal(@user) > end > it "should build a new ticket on the user" do > > @user_tickets_association.should_receive(:build).with(@params).and_return(@ticket) > do_post > end > it "should assign the ticket for the view" do > do_post > assigns[:ticket].should equal(@ticket) > end > it "should save the new ticket" do > @ticket.should_receive(:save).with().and_return(true) > do_post > end > > it "should save the new ticket" do > @ticket.should_receive(:save).with().and_return(true) > do_post > end > > it "should redirect to the ticket path if successful" do > do_post > response.should redirect_to(''/tickets'') > end > > it "should render the new page if create fails" do > @ticket.stub!(:save).and_return(false) > do_post > response.should render_template(''new'') > end > end > > > > On 18 Aug 2007, at 00:36, Andrew WC Brown wrote: > > That makes a-lot of sense. That ''new'' word threw me off. > > My create method is slightly more complex and I''m not sure how to spec it. > > def create > @user = User.find(params[:user_id]) > @ticket = @ user.tickets.build(params[:ticket]) > if @ticket.save > redirect_to tickets_path > else > render new_usr_ticket_path(params[:user_id]) > end > end > > > Would I need two mock_models? > > On 8/17/07, rupert <rupert_apsc at rupespad.com> wrote: > > > > what''s in the create method of the controller?? you''ve only given the > > contents of the new method - and without knowing what you want it to do it''s > > hard to know what will work. > > Assuming it''s the normal > > > > def create > > @ticket = Ticket.new(params(:ticket]) > > if @ticket.save > > ....etc etc > > end > > > > then something like the following should work: > > > > describe TicketsController, "handling POST /tickets" do > > before do > > @ticket = mock_model(Ticket, :save => true) > > Ticket.stub!(:new).and_return(@ticket) > > @params = {} > > end > > > > def do_post > > post :create, :ticket => @params > > end > > > > it "should create a new ticket" do > > ticket.should_receive(:new).with(@params).and_return(@ticket) > > do_post > > end > > > > it "should assign the ticket for the view" do > > do_post > > assigns[:ticket].should equal(@ticket) > > end > > > > it "should save the new ticket" do > > @ticket.should_receive(:save).with().and_return(true) > > do_post > > end > > end > > > > On 18 Aug 2007, at 00:11, Andrew WC Brown wrote: > > > > I replaced: > > > > @ticket.should_receive (:new).with(@params).and_return(@ticket) > > > > with > > > > Ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > Similar Error > > > > 1) > > Spec::Mocks::MockExpectationError in ''TicketsController handling POST > > /tickets should create a new ticket'' > > Mock ''Class'' expected :new with ({}) once, but received it 0 times > > ./spec/controllers/tickets_controller_spec.rb:16: > > > > If I omit @params = {} then I get the error as following > > > > 1) > > Spec::Mocks::MockExpectationError in ''TicketsController handling POST > > /tickets should create a new ticket'' > > Mock ''Class'' expected :new with (any args) once, but received it 0 times > > ./spec/controllers/tickets_controller_spec.rb:16: > > > > So its not receiving the new method at all even though I clearly called > > it. > > Could it be possible that the issue is with the actual Ticket model > > itself? > > > > On 8/17/07, David Chelimsky < dchelimsky at gmail.com > wrote: > > > > > > On 8/17/07, Andrew WC Brown < omen.king at gmail.com > wrote: > > > > I''ve been off the rspec for a few months and I''m trying to get back > > > on it. > > > > > > > > 1) > > > > Spec::Mocks::MockExpectationError in ''TicketsController > > > > handling POST /tickets should create a new ticket'' > > > > Mock ''Ticket_1001'' expected :new with ({}) once, but received it 0 > > > times > > > > ./spec/controllers/tickets_controller_spec.rb:16: > > > > script/spec:4: > > > > > > > > class TicketsController < ApplicationController > > > > > > > > def new > > > > Ticket.new > > > > end > > > > > > > > end > > > > > > > > describe TicketsController, "handling POST /tickets" do > > > > > > > > before do > > > > @ticket = mock_model(Ticket, :to_param => ''1'', :save => true) > > > > Ticket.stub!(:new).and_return(@ticket) > > > > @params = {} > > > > end > > > > > > > > def do_post > > > > post :create, :ticket => @params > > > > end > > > > > > > > it "should create a new ticket" do > > > > > > > > @ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > > > This is telling the ticket object to expect new, but it''s the Ticket > > > class that will receive it: > > > > > > Ticket.should_receive(:new).with(@params).and_return(@ticket) > > > > > > That should do it. > > > > > > > do_post > > > > end > > > > > > > > end > > > > > > > > Would someone provide with an explanation what I have to do to make > > > this > > > > spec pass? > > > > Peepcode hasn''t released their screen cast on rspecing controllers > > > yet =''( > > > > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > 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 > > > > > > > > > > > -- > > Monsterbox Productions > > putting small businesses on-line > > > > 1319 Victoria Avenue East > > Thunder Bay, Ontario P7C 1C3 > > Canada > > > > Andrew WC Brown > > web-developer and owner > > andrew at monsterboxpro.com > > P: 807-626-9009 > > F: 807-624-2705_______________________________________________ > > 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 > > > > > > -- > Monsterbox Productions > putting small businesses on-line > > 1319 Victoria Avenue East > Thunder Bay, Ontario P7C 1C3 > Canada > > Andrew WC Brown > web-developer and owner > andrew at monsterboxpro.com > P: 807-626-9009 > F: 807-624-2705_______________________________________________ > 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 >-- Monsterbox Productions putting small businesses on-line 1319 Victoria Avenue East Thunder Bay, Ontario P7C 1C3 Canada Andrew WC Brown web-developer and owner andrew at monsterboxpro.com P: 807-626-9009 F: 807-624-2705 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070817/9cba7847/attachment.html
Rupert Voelcker
2007-Aug-18 00:08 UTC
[rspec-users] [rspec] looking for a simple refresher
> thats pretty much what I thought, I''ll give it a push around and e- > mail back the resultscool - good luck ...I''m off to bed as it''s 1am here in the UK but I''m sure one of the useful chaps round here ''ll help you out with any probs btw, I just noticed I''ve put @ticket_params = {} in the before declaration and it needs to be @params = {} Cheers
Andrew WC Brown
2007-Aug-20 17:17 UTC
[rspec-users] [rspec] looking for a simple refresher
Still having trouble =''(
def create
@user = User.find(params[:user_id])
@ticket = @user.tickets.build(params[:ticket])
if @ticket.save
redirect_to tickets_path
else
render new_usr_ticket_path(params[:user_id])
end
end
describe TicketsController, "handling POST /tickets" do
before do
@ticket = mock_model(Ticket, :save => true)
@user = mock_model(User)
@user.stub!(:tickets).and_return(@user_tickets_association)
@params = {}
end
def do_post
post :create, :ticket => @params, :user_id => "user_id"
end
it "should find the user" do
User.should_receive(:find).with("user_id").and_return(@user)
do_post
end
8)
Spec::Mocks::MockExpectationError in ''TicketsController handling POST
/tickets should find the user''
Mock ''Class'' expected :find with ("user_id") once,
but received it 0 times
./spec/controllers/tickets_controller_spec.rb:19:
script/spec:4:
That really looks like it should pass.
On 8/17/07, Rupert Voelcker <rupespad1 at mac.com>
wrote:>
> > thats pretty much what I thought, I''ll give it a push around
and e-
> > mail back the results
>
> cool - good luck ...I''m off to bed as it''s 1am here in
the UK but I''m
> sure one of the useful chaps round here ''ll help you out with any
probs
>
> btw, I just noticed I''ve put @ticket_params = {} in the before
> declaration and it needs to be @params = {}
>
> Cheers
>
>
>
> _______________________________________________
> 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/20070820/7d32e6cf/attachment.html
Andrew WC Brown
2007-Aug-20 17:42 UTC
[rspec-users] [rspec] looking for a simple refresher
I solved my problem.
def create
@ticket = Ticket.new(params[:ticket])
@ticket.user = current_user
if @ticket.save
redirect_to tickets_path
else
render new_ticket_path(params[:user_id])
end
end
Like my mom would nag:
"If you rspec''d from the start you wouldn''t have to clean
up this mess"
On 8/20/07, Andrew WC Brown <omen.king at gmail.com>
wrote:>
> Still having trouble =''(
>
>
> def create
> @user = User.find(params[:user_id])
> @ticket = @user.tickets.build(params[:ticket])
> if @ticket.save
> redirect_to tickets_path
> else
> render new_usr_ticket_path(params[:user_id])
> end
> end
>
> describe TicketsController, "handling POST /tickets" do
> before do
> @ticket = mock_model(Ticket, :save => true)
> @user = mock_model(User)
> @user.stub!(:tickets).and_return(@user_tickets_association)
> @params = {}
> end
> def do_post
> post :create, :ticket => @params, :user_id =>
"user_id"
> end
> it "should find the user" do
> User.should_receive(:find).with("user_id").and_return(@user)
> do_post
> end
>
> 8)
> Spec::Mocks::MockExpectationError in ''TicketsController handling
POST
> /tickets should find the user''
> Mock ''Class'' expected :find with ("user_id")
once, but received it 0 times
> ./spec/controllers/tickets_controller_spec.rb:19:
> script/spec:4:
>
>
> That really looks like it should pass.
>
>
> On 8/17/07, Rupert Voelcker <rupespad1 at mac.com> wrote:
> >
> > > thats pretty much what I thought, I''ll give it a push
around and e-
> > > mail back the results
> >
> > cool - good luck ...I''m off to bed as it''s 1am here
in the UK but I''m
> > sure one of the useful chaps round here ''ll help you out with
any probs
> >
> > btw, I just noticed I''ve put @ticket_params = {} in the
before
> > declaration and it needs to be @params = {}
> >
> > Cheers
> >
> >
> >
> > _______________________________________________
> > 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/20070820/b6c699e4/attachment.html
Seemingly Similar Threads
- using restful_authentication current_user inside controller specs
- spec''ing view render partial collection, local variable not found
- I''m really bad at controllers, help please.
- Agh, this is annoying. Why is this happening?
- Problems with testing nested routes using mocking