A controller I''m trying to test simply delivers a text string to the
client, which then demarshalls it to retrieve some objects. I want to
test that the returned string is correct.
I don''t want to compare the string character-by-character with
response.has_text because that ties me to the implementation of the
Marshall class. Instead, I just want to demarshall the string and
compare that with the expected objects. To do this, I need RSpec to
give me the complete text string that''s rendered by the controller.
Here''s the controller:
class StatsController < ApplicationController
layout nil
def query
reports = StatisticsReporter.query params[:query_params]
render :text => Marshal.dump(reports)
end
end
And here''s the test:
describe StatsController do
before :each do
@query_params = { :foo => :bar }
end
it "should return a demarshallable string of the reports" do
reports = [
StatisticsReport.new(:date => nil),
StatisticsReport.new(:bytes_transferred => 0)
]
StatisticsReporter.should_receive(:query).and_return reports
get :query, :query_params => @query_params
# PROBLEM CODE (the equality test needs tweaking, but you get the
idea)
Marshal.load(response.what_goes_here?).should == reports
end
end
(This code may look familiar to one member of this list...:)
Can this be done?
adthanksvance,
///ark
On May 8, 2008, at 5:26 PM, Mark Wilden wrote:> A controller I''m trying to test simply delivers a text string to the > client, which then demarshalls it to retrieve some objects. I want > to test that the returned string is correct. > > I don''t want to compare the string character-by-character with > response.has_text because that ties me to the implementation of the > Marshall class. Instead, I just want to demarshall the string and > compare that with the expected objects. To do this, I need RSpec to > give me the complete text string that''s rendered by the controller. > > Here''s the controller: > > class StatsController < ApplicationController > layout nil > > def query > reports = StatisticsReporter.query params[:query_params] > render :text => Marshal.dump(reports) > end > end > > And here''s the test: > > describe StatsController do > before :each do > @query_params = { :foo => :bar } > end > > it "should return a demarshallable string of the reports" do > reports = [ > StatisticsReport.new(:date => nil), > StatisticsReport.new(:bytes_transferred => 0) > ] > StatisticsReporter.should_receive(:query).and_return reports > > get :query, :query_params => @query_params > > # PROBLEM CODE (the equality test needs tweaking, but you get the > idea) > Marshal.load(response.what_goes_here?).should == reportsTry this: Marshal.load(response.body).should == reports> > end > end > > (This code may look familiar to one member of this list...:) > > Can this be done? > > adthanksvance, > > ///ark > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On May 8, 2008, at 3:38 PM, David Chelimsky wrote:>> I need RSpec to give me the complete text string that''s rendered by >> the controller. >> >> Try this: > > Marshal.load(response.body).should == reportsDang! I dumped response.methods and tried everything that looked likely, but I guess I missed that one. Thanks! Just to preserve my sanity in the future, is there a place where this is documented? ///ark
Maybe Matching Threads
- form problem on deployment. Not sure how to debug this.
- Rendering all the elements when calling Array#to_xml
- self referential (n-n relationship) belongs many controller
- response.should have_text leads to undefined method `has_text?'
- params not getting POSTed