How can I test the send_data
method i开发者_开发技巧n Rails?
First of all look at the source of the send_data
method http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
According to that, send_data
simply put everything to render :text => '...'
with additional options.
I think you can do it in this way:
response.body.should eql data
response.header['Content-Type'].should eql 'image/png'
You shouldn't need to test the behaviour of send_data
itself, mainly because that's covered by Rails' own tests. Also, it will make your tests run slowly (eventually). What you should do (from my perspective) is to stub the send_data method, something like:
controller.expects(:send_data).with("foo").returns(:success)
Hope it helps.
You can test it indirectly by checking the value of Content-Transfer-Encoding
header.
expect(controller.headers["Content-Transfer-Encoding"]).to eq("binary")
or
controller.headers["Content-Transfer-Encoding"].should eq("binary")
When I read his question I took it to mean, how does he make sure that send_data sent the string/whatever he asked it to. Not so much to test it's sending but to ensure for his peace of mind that the method he's sent it wasn't blank. mocking, as you've done, doesn't really get him that result.
Perhaps you can ensure that your string isn't blank, or something like that. This way you aren't testing send_data, but that whatever send_data gets is how you want it to look.
In my case (what brought me to this question) would be
#just use this to make sure it looks like you want it to while you are writing your
#tests. I remove it after. make sure it's an instance variable @csv_string in my case.
puts assigns(:csv_string)
refute_nil assigns(:csv_string) #does the actual work. delete the puts line when done.
Some fancier people use ruby debugger and sh!t... your mileage will vary.
For a minitest
version answer:
assert_equal("application/json", response.header["Content-Type"])
assert_equal(expected_response, response.body)
精彩评论