How test the send_data method in Rails?

一笑奈何 提交于 2019-12-21 07:21:44

问题


How can I test the send_data method in Rails?


回答1:


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.




回答2:


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'



回答3:


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.




回答4:


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")


来源:https://stackoverflow.com/questions/3063433/how-test-the-send-data-method-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!