Hello, I am learning rspec/rspec-rails and ruby on rails.
Controller:
class IssuesController < ApplicationController
def index
end
def new
@issue = Issue.new
end
def create
@issue = Issue.new(params[:issue])
@issue.save
end
end
Spec:
require File.expand_path(File.join(File.dirname(__FILE__),
''../spec_helper.rb''))
describe IssuesController do
it ''should sucess on index'' do
get :index
response.should be_success
end
describe ''create new issue'' do
it ''should success on new'' do
get :new
response.should be_success
end
it ''should pass a new Issue to the view'' do
issue = mock_model(Issue)
Issue.stub!(:new).and_return(issue)
get :new
assigns[:issue].should == issue
end
end
describe ''saving with right data'' do
it ''should save the issue'' do
issue = mock_model(Issue)
Issue.stub!(:new).and_return(issue)
issue.stub!(:save).and_return(:true)
post :create, { :issue => {:title => ''some title'',
:description =>
''some more data to describe''}}
assigns[:issue].should == issue
end
end
describe ''when saving wrong data'' do
# before(:each) do
# @issue = mock_model(Issue)
# @issue.stub!(:save).and_return(:false)
# Issue.stub!(:new).and_return(@issue)
# end
# def post_create
# post :create, { :issue => {:title => ''some title'',
:description =>
''some more data to describe''}}
# end
it ''should not save the issue'' do
@issue = mock_model(Issue)
@issue.stub!(:save).and_return(:false)
Issue.stub!(:new).and_return(@issue)
post :create, { :issue => {:title => ''some title'',
:description => ''''}}
@issue.should_receive(:save).and_return(:false)
end
# it ''should flash the failure to save'' do
# end
end
end
Error:
F....
1)
Spec::Mocks::MockExpectationError in ''IssuesController when saving
wrong data should not save the issue''
Mock ''Issue_1001'' expected :save with (any args) once, but
received it 0 times
spec/controllers/issues_controler_spec.rb:51:
spec/controllers/issues_controler_spec.rb:3:
Finished in 0.282526 seconds
5 examples, 1 failure
I am running script/autospec.
?What is wrong? I don''t understand why this spec is not passing. I
have tried before(:each) and a helper method, but got the same.
Please, help.
MaurĂcio Linhares
2008-Jul-23 01:43 UTC
[rspec-users] [HELP] should receive :new but does not
The "should_receive" should appear before the "post :create"
(look at
this for a better explanation ->
http://rubyforge.org/pipermail/rspec-users/2008-June/007342.html )
it ''should not save the issue'' do
@issue = mock_model(Issue)
@issue.stub!(:save).and_return(:false)
Issue.stub!(:new).and_return(@issue)
@issue.should_receive(:save).and_return(:false)
# you should define the "should receive" before calling "post
:create"
post :create, { :issue => {:title => ''some
title'', :description => ''''}}
end
On Tue, Jul 22, 2008 at 10:36 PM, Camilo Torres <xtrmclmb at gmail.com>
wrote:>
> Hello, I am learning rspec/rspec-rails and ruby on rails.
>
> Controller:
> class IssuesController < ApplicationController
> def index
>
> end
>
> def new
> @issue = Issue.new
> end
>
> def create
> @issue = Issue.new(params[:issue])
> @issue.save
> end
> end
>
> Spec:
> require File.expand_path(File.join(File.dirname(__FILE__),
''../spec_helper.rb''))
>
> describe IssuesController do
> it ''should sucess on index'' do
> get :index
> response.should be_success
> end
>
> describe ''create new issue'' do
> it ''should success on new'' do
> get :new
> response.should be_success
> end
>
> it ''should pass a new Issue to the view''
do
> issue = mock_model(Issue)
> Issue.stub!(:new).and_return(issue)
> get :new
> assigns[:issue].should == issue
> end
> end
>
> describe ''saving with right data'' do
> it ''should save the issue'' do
> issue = mock_model(Issue)
> Issue.stub!(:new).and_return(issue)
> issue.stub!(:save).and_return(:true)
> post :create, { :issue => {:title =>
''some title'', :description =>
> ''some more data to describe''}}
> assigns[:issue].should == issue
> end
> end
>
> describe ''when saving wrong data'' do
> # before(:each) do
> # @issue = mock_model(Issue)
> # @issue.stub!(:save).and_return(:false)
> # Issue.stub!(:new).and_return(@issue)
> # end
>
> # def post_create
> # post :create, { :issue => {:title =>
''some title'', :description =>
> ''some more data to describe''}}
> # end
>
> it ''should not save the issue'' do
> @issue = mock_model(Issue)
> @issue.stub!(:save).and_return(:false)
> Issue.stub!(:new).and_return(@issue)
>
> post :create, { :issue => {:title =>
''some title'', :description => ''''}}
>
> @issue.should_receive(:save).and_return(:false)
> end
>
> # it ''should flash the failure to save'' do
>
> # end
> end
> end
>
> Error:
> F....
>
> 1)
> Spec::Mocks::MockExpectationError in ''IssuesController when saving
> wrong data should not save the issue''
> Mock ''Issue_1001'' expected :save with (any args) once,
but received it 0 times
> spec/controllers/issues_controler_spec.rb:51:
> spec/controllers/issues_controler_spec.rb:3:
>
> Finished in 0.282526 seconds
>
> 5 examples, 1 failure
>
>
>
> I am running script/autospec.
>
> ?What is wrong? I don''t understand why this spec is not passing. I
> have tried before(:each) and a helper method, but got the same.
>
> Please, help.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
--
Maur?cio Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
Jo?o Pessoa, PB, +55 83 8867-7208
thanks Maur?cio, I read your link. It worked. 2008/7/23 Maur?cio Linhares <mauricio.linhares at gmail.com>:> The "should_receive" should appear before the "post :create" (look at > this for a better explanation -> > http://rubyforge.org/pipermail/rspec-users/2008-June/007342.html ) > > it ''should not save the issue'' do > @issue = mock_model(Issue) > @issue.stub!(:save).and_return(:false) > > Issue.stub!(:new).and_return(@issue) > @issue.should_receive(:save).and_return(:false) > # you should define the "should receive" before calling "post :create" > > post :create, { :issue => {:title => ''some > title'', :description => ''''}} > end > > On Tue, Jul 22, 2008 at 10:36 PM, Camilo Torres <xtrmclmb at gmail.com> wrote: >> >> Hello, I am learning rspec/rspec-rails and ruby on rails. >> >> Controller: >> class IssuesController < ApplicationController >> def index >> >> end >> >> def new >> @issue = Issue.new >> end >> >> def create >> @issue = Issue.new(params[:issue]) >> @issue.save >> end >> end >> >> Spec: >> require File.expand_path(File.join(File.dirname(__FILE__), ''../spec_helper.rb'')) >> >> describe IssuesController do >> it ''should sucess on index'' do >> get :index >> response.should be_success >> end >> >> describe ''create new issue'' do >> it ''should success on new'' do >> get :new >> response.should be_success >> end >> >> it ''should pass a new Issue to the view'' do >> issue = mock_model(Issue) >> Issue.stub!(:new).and_return(issue) >> get :new >> assigns[:issue].should == issue >> end >> end >> >> describe ''saving with right data'' do >> it ''should save the issue'' do >> issue = mock_model(Issue) >> Issue.stub!(:new).and_return(issue) >> issue.stub!(:save).and_return(:true) >> post :create, { :issue => {:title => ''some title'', :description => >> ''some more data to describe''}} >> assigns[:issue].should == issue >> end >> end >> >> describe ''when saving wrong data'' do >> # before(:each) do >> # @issue = mock_model(Issue) >> # @issue.stub!(:save).and_return(:false) >> # Issue.stub!(:new).and_return(@issue) >> # end >> >> # def post_create >> # post :create, { :issue => {:title => ''some title'', :description => >> ''some more data to describe''}} >> # end >> >> it ''should not save the issue'' do >> @issue = mock_model(Issue) >> @issue.stub!(:save).and_return(:false) >> Issue.stub!(:new).and_return(@issue) >> >> post :create, { :issue => {:title => ''some title'', :description => ''''}} >> >> @issue.should_receive(:save).and_return(:false) >> end >> >> # it ''should flash the failure to save'' do >> >> # end >> end >> end >> >> Error: >> F.... >> >> 1) >> Spec::Mocks::MockExpectationError in ''IssuesController when saving >> wrong data should not save the issue'' >> Mock ''Issue_1001'' expected :save with (any args) once, but received it 0 times >> spec/controllers/issues_controler_spec.rb:51: >> spec/controllers/issues_controler_spec.rb:3: >> >> Finished in 0.282526 seconds >> >> 5 examples, 1 failure >> >> >> >> I am running script/autospec. >> >> ?What is wrong? I don''t understand why this spec is not passing. I >> have tried before(:each) and a helper method, but got the same. >> >> Please, help. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > -- > Maur?cio Linhares > http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) > Jo?o Pessoa, PB, +55 83 8867-7208 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >