Hi:
I am a rspec beginner. I have scratched my head for the whole afternoon,
trying to figure out a way of writing render expectation in controller spec.
However, I failed. It kept telling me that the ''expect_render''
was an
undefined method. I have installed rspec 1.0.8 and rspec_on_rails as well.
Below is the controller to be tested.
Controller
def index
pre = Array.new
render :partial=>''index'', :layout=>false,
:locals=>{:pre=>pre}
end
My controller spec
before do
@controller = MyController.new
end
it "should render the index partial" do
@controller.expect_render(:partial=>''index'',
:layout=>false) #I don''t
know how to expect the :locals
end
As I mentioned, the spec failed , saying that "Undefined method
''expect_render'' ". There might be some mistakes in my
"before" block. Do I
need to mock the controller or something else? Anyway, do any one know how
to fix the problem? It will be even better if you guys could show me a piece
of example code.
Thanks!!
--
View this message in context:
http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12121894
Sent from the rspec-users mailing list archive at Nabble.com.
On 8/13/07, Shaker <flyeminent at hotmail.com> wrote:> > Hi: > I am a rspec beginner. I have scratched my head for the whole afternoon, > trying to figure out a way of writing render expectation in controller spec. > However, I failed. It kept telling me that the ''expect_render'' was an > undefined method. I have installed rspec 1.0.8 and rspec_on_rails as well. > Below is the controller to be tested. > > Controller > def index > pre = Array.new > render :partial=>''index'', :layout=>false, :locals=>{:pre=>pre} > end > > My controller spec > before do > @controller = MyController.new > end > it "should render the index partial" do > @controller.expect_render(:partial=>''index'', :layout=>false) #I don''t > know how to expect the :locals > end > > As I mentioned, the spec failed , saying that "Undefined method > ''expect_render'' ". There might be some mistakes in my "before" block. Do I > need to mock the controller or something else? Anyway, do any one know how > to fix the problem? It will be even better if you guys could show me a piece > of example code. > Thanks!!Have you looked here? http://rspec.rubyforge.org/documentation/rails/writing/views.html (you must use template.expect_render) Or perhaps you''re looking for response.should render_template("path/to/template/for/action") HTH, Aslak> -- > View this message in context: http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12121894 > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Hello, Aslak:
I know we could do template.expect_render somehow, although I
haven''t
tried that out. But the rspec developer said that we could now actually use
controller.expect_render in controller specs, which caused me confused. You
may want to have a look at this:
http://rspec.rubyforge.org/documentation/rails/writing/controllers.html
writing controller spec
Thanks, regards.
aslak hellesoy wrote:>
> On 8/13/07, Shaker <flyeminent at hotmail.com> wrote:
>>
>> Hi:
>> I am a rspec beginner. I have scratched my head for the whole
>> afternoon,
>> trying to figure out a way of writing render expectation in controller
>> spec.
>> However, I failed. It kept telling me that the
''expect_render'' was an
>> undefined method. I have installed rspec 1.0.8 and rspec_on_rails as
>> well.
>> Below is the controller to be tested.
>>
>> Controller
>> def index
>> pre = Array.new
>> render :partial=>''index'', :layout=>false,
:locals=>{:pre=>pre}
>> end
>>
>> My controller spec
>> before do
>> @controller = MyController.new
>> end
>> it "should render the index partial" do
>> @controller.expect_render(:partial=>''index'',
:layout=>false) #I
>> don''t
>> know how to expect the :locals
>> end
>>
>> As I mentioned, the spec failed , saying that "Undefined method
>> ''expect_render'' ". There might be some mistakes
in my "before" block. Do
>> I
>> need to mock the controller or something else? Anyway, do any one know
>> how
>> to fix the problem? It will be even better if you guys could show me a
>> piece
>> of example code.
>> Thanks!!
>
> Have you looked here?
>
> http://rspec.rubyforge.org/documentation/rails/writing/views.html
>
> (you must use template.expect_render)
> Or perhaps you''re looking for response.should
> render_template("path/to/template/for/action")
>
> HTH,
> Aslak
>
>> --
>> View this message in context:
>>
http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12121894
>> Sent from the rspec-users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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
>
>
--
View this message in context:
http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12122511
Sent from the rspec-users mailing list archive at Nabble.com.
On 8/13/07, Shaker <flyeminent at hotmail.com> wrote:> > Hello, Aslak: > I know we could do template.expect_render somehow, although I haven''t > tried that out. But the rspec developerAslak is one of the rspec developers. :)> said that we could now actually use > controller.expect_render in controller specs, which caused me confused. You > may want to have a look at this: > http://rspec.rubyforge.org/documentation/rails/writing/controllers.html > writing controller spec > Thanks, regards. > > aslak hellesoy wrote: > > > > On 8/13/07, Shaker <flyeminent at hotmail.com> wrote: > >> > >> Hi: > >> I am a rspec beginner. I have scratched my head for the whole > >> afternoon, > >> trying to figure out a way of writing render expectation in controller > >> spec. > >> However, I failed. It kept telling me that the ''expect_render'' was an > >> undefined method. I have installed rspec 1.0.8 and rspec_on_rails as > >> well. > >> Below is the controller to be tested. > >> > >> Controller > >> def index > >> pre = Array.new > >> render :partial=>''index'', :layout=>false, :locals=>{:pre=>pre} > >> end > >> > >> My controller spec > >> before do > >> @controller = MyController.new > >> end > >> it "should render the index partial" do > >> @controller.expect_render(:partial=>''index'', :layout=>false) #I > >> don''t > >> know how to expect the :locals > >> endDon''t create an instance of the controller directly. Use either: describe MyController do ... or describe "MyController" do controller_name :my ... RSpec mixes some methods into the controller in these cases (including expect_render) but does not in the case you describe because it initializes the controller directly. Cheers, David> >> > >> As I mentioned, the spec failed , saying that "Undefined method > >> ''expect_render'' ". There might be some mistakes in my "before" block. Do > >> I > >> need to mock the controller or something else? Anyway, do any one know > >> how > >> to fix the problem? It will be even better if you guys could show me a > >> piece > >> of example code. > >> Thanks!! > > > > Have you looked here? > > > > http://rspec.rubyforge.org/documentation/rails/writing/views.html > > > > (you must use template.expect_render) > > Or perhaps you''re looking for response.should > > render_template("path/to/template/for/action") > > > > HTH, > > Aslak > > > >> -- > >> View this message in context: > >> http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12121894 > >> Sent from the rspec-users mailing list archive at Nabble.com. > >> > >> _______________________________________________ > >> 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 > > > > > > -- > View this message in context: http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12122511 > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 8/13/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 8/13/07, Shaker <flyeminent at hotmail.com> wrote: > > > > Hello, Aslak: > > I know we could do template.expect_render somehow, although I haven''t > > tried that out. But the rspec developer > > Aslak is one of the rspec developers. :) > > > said that we could now actually use > > controller.expect_render in controller specs, which caused me confused. You > > may want to have a look at this: > > http://rspec.rubyforge.org/documentation/rails/writing/controllers.html > > writing controller spec > > Thanks, regards. > > > > aslak hellesoy wrote: > > > > > > On 8/13/07, Shaker <flyeminent at hotmail.com> wrote: > > >> > > >> Hi: > > >> I am a rspec beginner. I have scratched my head for the whole > > >> afternoon, > > >> trying to figure out a way of writing render expectation in controller > > >> spec. > > >> However, I failed. It kept telling me that the ''expect_render'' was an > > >> undefined method. I have installed rspec 1.0.8 and rspec_on_rails as > > >> well. > > >> Below is the controller to be tested. > > >> > > >> Controller > > >> def index > > >> pre = Array.new > > >> render :partial=>''index'', :layout=>false, :locals=>{:pre=>pre} > > >> end > > >> > > >> My controller spec > > >> before do > > >> @controller = MyController.new > > >> end > > >> it "should render the index partial" do > > >> @controller.expect_render(:partial=>''index'', :layout=>false) #I > > >> don''t > > >> know how to expect the :locals > > >> end > > Don''t create an instance of the controller directly. Use either: > > describe MyController do > ... > > or > > describe "MyController" do > controller_name :my > ... >Doh, I missed that.> RSpec mixes some methods into the controller in these cases (including > expect_render) but does not in the case you describe because it > initializes the controller directly. >I wonder if this is a common mistake among newcomers to RSpec (since Rails/Test::Unit does it that way). I don''t think we should build in any warning mechanisms, but I''ll update the page mentioning this. Aslak> Cheers, > David > > > > >> > > >> As I mentioned, the spec failed , saying that "Undefined method > > >> ''expect_render'' ". There might be some mistakes in my "before" block. Do > > >> I > > >> need to mock the controller or something else? Anyway, do any one know > > >> how > > >> to fix the problem? It will be even better if you guys could show me a > > >> piece > > >> of example code. > > >> Thanks!! > > > > > > Have you looked here? > > > > > > http://rspec.rubyforge.org/documentation/rails/writing/views.html > > > > > > (you must use template.expect_render) > > > Or perhaps you''re looking for response.should > > > render_template("path/to/template/for/action") > > > > > > HTH, > > > Aslak > > > > > >> -- > > >> View this message in context: > > >> http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12121894 > > >> Sent from the rspec-users mailing list archive at Nabble.com. > > >> > > >> _______________________________________________ > > >> 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 > > > > > > > > > > -- > > View this message in context: http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12122511 > > Sent from the rspec-users mailing list archive at Nabble.com. > > > > _______________________________________________ > > 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 >
Dear Aslak and David:
I have tried two possible ways to get expect_render work. However, they
all failed. My combinations are:
my controller spec
describe MyController do
before do
#do nothing
end
it "should render the index partial" do
controller.expect_render(:partial=>''index'') #1st way
MyController.expect_render(:partial=>''index'') #2nd
way
end
end
The 1st way gave me an error: Mock ''expect_render_mock_proxy''
expected
:render with ({:partial=>''index''}) once, but received it 0
times
The 2nd way gave me an error: undefined method ''expect_render''
Do I need to mock the controller or there are some mistakes I have made in
my spec?
Shaker wrote:>
> Hi:
> I am a rspec beginner. I have scratched my head for the whole
> afternoon, trying to figure out a way of writing render expectation in
> controller spec. However, I failed. It kept telling me that the
> ''expect_render'' was an undefined method. I have installed
rspec 1.0.8 and
> rspec_on_rails as well. Below is the controller to be tested.
>
> Controller
> def index
> pre = Array.new
> render :partial=>''index'', :layout=>false,
:locals=>{:pre=>pre}
> end
>
> My controller spec
> before do
> @controller = MyController.new
> end
> it "should render the index partial" do
> @controller.expect_render(:partial=>''index'',
:layout=>false) #I
> don''t know how to expect the :locals
> end
>
> As I mentioned, the spec failed , saying that "Undefined method
> ''expect_render'' ". There might be some mistakes in
my "before" block. Do I
> need to mock the controller or something else? Anyway, do any one know how
> to fix the problem? It will be even better if you guys could show me a
> piece of example code.
> Thanks!!
>
--
View this message in context:
http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12123224
Sent from the rspec-users mailing list archive at Nabble.com.
On 8/13/07, Shaker <flyeminent at hotmail.com> wrote:> > Dear Aslak and David: > I have tried two possible ways to get expect_render work. However, they > all failed. My combinations are: > my controller spec > describe MyController do > before do > #do nothing > end > it "should render the index partial" do > controller.expect_render(:partial=>''index'') #1st wayThat''s the way to go> MyController.expect_render(:partial=>''index'') #2nd way > end > end > > The 1st way gave me an error: Mock ''expect_render_mock_proxy'' expected > :render with ({:partial=>''index''}) once, but received it 0 timesYou''ve forgotten to run the action. RSpec doesn''t magically guess what action is supposed to render your index partial. it "should render the index partial" do controller.expect_render(:partial=>''index'') get ''index'' end> The 2nd way gave me an error: undefined method ''expect_render'' > Do I need to mock the controller or there are some mistakes I have made in > my spec? > > Shaker wrote: > > > > Hi: > > I am a rspec beginner. I have scratched my head for the whole > > afternoon, trying to figure out a way of writing render expectation in > > controller spec. However, I failed. It kept telling me that the > > ''expect_render'' was an undefined method. I have installed rspec 1.0.8 and > > rspec_on_rails as well. Below is the controller to be tested. > > > > Controller > > def index > > pre = Array.new > > render :partial=>''index'', :layout=>false, :locals=>{:pre=>pre} > > end > > > > My controller spec > > before do > > @controller = MyController.new > > end > > it "should render the index partial" do > > @controller.expect_render(:partial=>''index'', :layout=>false) #I > > don''t know how to expect the :locals > > end > > > > As I mentioned, the spec failed , saying that "Undefined method > > ''expect_render'' ". There might be some mistakes in my "before" block. Do I > > need to mock the controller or something else? Anyway, do any one know how > > to fix the problem? It will be even better if you guys could show me a > > piece of example code. > > Thanks!! > > > > -- > View this message in context: http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12123224 > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 8/13/07, Shaker <flyeminent at hotmail.com> wrote:> > Dear Aslak: > Sorry to bother you over and over again. I did follow your instruction to > write the controller spec, but...the thing is not getting better.:-( > I''d like to post my code again, hopefully you would figure it out. > My Controller > class MyController > def index > pre = Array.new > render :partial=>''index'', layout=>false, locals=>{:pre=>pre} > end > end > ------------------------------------------------------------ > My Controller spec > describe MyController do > it "should render the index partial" do > controller.expect_render(:partial=>''index'', :layout=>false) > get ''index'' > end > end > ------------------------------------------------------------ > By the way, I am using JRuby rather than ruby. Hence my command to run the > spec above is > jruby --command spec spec/controllers/my_controller_spec.rb --format specdoc > It always gave me an error:Mock ''expect_render_mock_proxy'' expected :render > with > ({:partial=>"index"}) once, but received it 0 times. > Is it because "expect_render" is not compatible with JRuby or I forgot to > mock something? > Thanks a lot. > Regards. >Your failing test case seems to indicate a correct failure. You are expecting: controller.expect_render(:partial=>''index'', :layout=>false) but you are passing in: render :partial=>''index'', layout=>false, locals=>{:pre=>pre} Try updating your test to expect the :locals or try removing them from your call to render, whichever is correct for what you are doing, Zach
Dear Fellows:
Sorry to bother you over and over again. I did follow your instruction to
write the controller spec, but...the thing is not getting better.:-(
I''d like to post my code again, hopefully you would figure it out.
My Controller
class MyController
def index
pre = Array.new
render :partial=>''index'', :layout=>false,
:locals=>{:pre=>pre}
end
end
Notes: sorry, I just missed the colons for layout and locals, but I am
afraid that the spec still failed even I added in the colons.
------------------------------------------------------------
My Controller spec
describe MyController do
it "should render the index partial" do
controller.expect_render(:partial=>''index'',
:layout=>false)
get ''index''
end
end
------------------------------------------------------------
By the way, I am using JRuby rather than ruby. Hence my command to run the
spec above is
jruby --command spec spec/controllers/my_controller_spec.rb --format specdoc
It always gave me an error:Mock ''expect_render_mock_proxy''
expected :render
with
({:partial=>"index"}) once, but received it 0 times.
Is it because "expect_render" is not compatible with JRuby or I forgot
to
mock something?
Thanks a lot.
Regards.
aslak hellesoy wrote:>
> On 8/13/07, Shaker <flyeminent at hotmail.com> wrote:
>>
>> Dear Aslak and David:
>> I have tried two possible ways to get expect_render work. However,
>> they
>> all failed. My combinations are:
>> my controller spec
>> describe MyController do
>> before do
>> #do nothing
>> end
>> it "should render the index partial" do
>> controller.expect_render(:partial=>''index'')
#1st way
>
> That''s the way to go
>
>>
MyController.expect_render(:partial=>''index'') #2nd way
>> end
>> end
>>
>> The 1st way gave me an error: Mock
''expect_render_mock_proxy'' expected
>> :render with ({:partial=>''index''}) once, but
received it 0 times
>
> You''ve forgotten to run the action. RSpec doesn''t
magically guess what
> action is supposed to render your index partial.
>
> it "should render the index partial" do
> controller.expect_render(:partial=>''index'')
> get ''index''
> end
>
>
>> The 2nd way gave me an error: undefined method
''expect_render''
>> Do I need to mock the controller or there are some mistakes I have made
>> in
>> my spec?
>>
>> Shaker wrote:
>> >
>> > Hi:
>> > I am a rspec beginner. I have scratched my head for the whole
>> > afternoon, trying to figure out a way of writing render
expectation in
>> > controller spec. However, I failed. It kept telling me that the
>> > ''expect_render'' was an undefined method. I have
installed rspec 1.0.8
>> and
>> > rspec_on_rails as well. Below is the controller to be tested.
>> >
>> > Controller
>> > def index
>> > pre = Array.new
>> > render :partial=>''index'',
:layout=>false, :locals=>{:pre=>pre}
>> > end
>> >
>> > My controller spec
>> > before do
>> > @controller = MyController.new
>> > end
>> > it "should render the index partial" do
>> >
@controller.expect_render(:partial=>''index'',
:layout=>false) #I
>> > don''t know how to expect the :locals
>> > end
>> >
>> > As I mentioned, the spec failed , saying that "Undefined
method
>> > ''expect_render'' ". There might be some
mistakes in my "before" block.
>> Do I
>> > need to mock the controller or something else? Anyway, do any one
know
>> how
>> > to fix the problem? It will be even better if you guys could show
me a
>> > piece of example code.
>> > Thanks!!
>> >
>>
>> --
>> View this message in context:
>>
http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12123224
>> Sent from the rspec-users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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
>
>
--
View this message in context:
http://www.nabble.com/How-to-use-expect_render--tf4259590.html#a12136984
Sent from the rspec-users mailing list archive at Nabble.com.