Please read for an updated status on RailsCasts:
Learn more or hide this
Wow, allmost like voodoo. Was just about to question how I would test my external web requests properly and bam you come with this episode ;). Awesome!
The thing I might add for asynchronous requests (or requests that can time out) searched a lot around and the only proper way to make a request timeout seems to be as follows:
def http_api_call( url_address, form_parameters )
uri = URI.parse( url_address )
#this shortcut works but we can't set the timeouts...
#response = Net::HTTP.get_response(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 3
http.read_timeout = 3
#request = Net::HTTP::Get.new(uri.request_uri)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data( form_parameters )
response = http.request(request)
result = response.body.to_s
result = "WARNING: Connection timeout to external url on "+uri.to_s
result = "WARNING: Connection refused to external url on "+uri.to_s
result #give back response string
Using the above way your rails process does not hang if the request fails it just times out (had that problem with a backend that would sometimes fail to respond).
Kind regards and keep up the great work!
More test driven development episodes, it's awesome!
By the way the above does a post request (can do gets too which is commented) and it parses the uri including a port which can be something like http://example.com:5000. Parameters is a hash like:
Thanks for this! - this is really useful - time/web mocking can make tests much much simpler and cleaner.
I'd also like to recommend WebMock
From what I gather, WebMock is a bit more fully-featured (it was even originally inspired by FakeWeb), and also works with non-Net::HTTP - based libraries such as EM-HTTP-Request
I can't remember now why I chose FakeWeb over WebMock when I used it in railscasts.com, but it definitely does look more full featured. Thanks for bringing this up. Time to research it.
Also have a look at VCR - a great testing tool for recording and playing back http interactions.
Thanks for the link! I do hope to cover VCR in a future episode which is more about testing with an external api.
Thanks for the introduction to Timecop. It didn't even occur to me that something like that would exist. Thanks!
Great screencast as always, thanks!
One possible improvement (at least for me) is the block usage of Timecop#freeze.
Instead of having to call #return explicitly, I prefer to put the frozen code in a block, like:
That way it automatically returns from the frozen time and also makes explicit which code is frozen via indentation.
I think it's still good to have a Timecop.return statement in spec_helper.rb in case something slips through, but I do think the block syntax is great for larger test cases where you want to isolate where it is frozen.
Excellent cast as always, and I really like the testing angle. I've got a question though and I was hoping you or anyone else could help me out. I fairly experienced in the app folder, but not quite as much in spec. But alas, I have taken on Ryan's challenge last week and haven't opened the browser since. It's been going great, and I have tons of test coverage, but I'm worried I may have too much - or at least redundant tests. Specifically, I see a lot of overlay between my integration tests and my controller tests. I find that whichever one I write first, it covers about 75% of the other one. That really bugs me. So a few things:
Thanks so much everyone.
I don't do controller or view tests because I feel they overlap too much with integration tests. Usually if something is too complex to go in an integration test then it should be moved into a model or helper and tested there in isolation. This makes it easier to handle a lot of branching paths and such as well.
I'm not very strict on refactoring test code, I feel it's better to be more direct there to reduce the possibility of bugs and odd dependencies. But when I do see a lot of duplication I like to move it into a module in the spec/support directory and do config.include in the spec_helper.rb
Ok interesting to know. I was trying to stick with that as well, but I really ran into trouble when I wanted to test redirects and urls. Maybe there's a way to do this (cleanly), but I couldn't figure it out. Say you wanted to test a very simple create action that redirects to the show page. In the integration test you have something like:
fill_in :name, :with => "Ken"
at this point, how can you access the controller variables to ensure that you redirect to user_path(@user). assigns(:user) wasn't working, and after days of searching, came to the conclusion this sort of test is best served in the controller specs. I supposed you could do something like:
but that didn't seem right at all.
Ryan Bates is a God!!
My first impulse when watching the first part of the episode was "just mock the Time.zone method so that Time.zone.now returns a fixed date"
Wouldn't that have been enough?
I thought exactly the same. I always just mock Time.zone.now (or Time.now) to test if the time gets set.
I really would like to hear what the benefits of using timecop are if you only want to test if the time gets set to Time.zone.now
Hi Ryan, fantastic episode as alwais.
Do You know a way to test file uploads? e.g size, timeout, type of files (mime), image dimensions,.. ?
And where to put this stub files?
Your just too awesome !! I am blind with true awesomeness :)
Testing with times involved is mostly no fun, particularly because I never heard of Timecop before :) But I found a way to manage it with RSpec matchers:
it "saves the time the password reset was sent" do
That's what I've been doing as well. I'm giving Timecop a try now and it's pretty good, not much changed except a few tests so far.
It can have unpredictable behaviour. What if send_password_reset first sets attribute value and then does something longer than a second due to high system load? What if send_password_reset doesn't really set attribute value but some previous test does and tests are not fully isolated? You'll have test that breaks from time to time in random manner.
You're right. The Timecop version is more deterministic and that is what our tests should be.
But there maybe situations where you can't use Timecop and you don't need to test on milliseconds (with an increased delta to get a more robust test), then it might be a good solution too.
I can't believe no one has taken to calling you Dr. Bates yet. Not just for your coding examples, but also because of your ESP in delivering these episodes exactly when others are in need of them. Thanks again!
I can't get the timecop to work in my application.
I hope you guys know what's wrong. I've used Ryans code.
What I see is that the reloaded object has nil for the password_reset_sent_at attribute.
1) User#send_password_reset saves the time the password reset was sent
Failure/Error: user.reload.password_reset_sent_at.should eq(Time.zone.now)
expected Fri, 31 Dec -0001 23:40:28 UTC +00:00
(compared using ==)
# ./spec/models/user_spec.rb:24:in `block (3 levels) in <top (required)>'
it "saves the time the password reset was sent" do
puts 'printing user password_reset_sent_at'
puts 'printing reloaded user password_reset_sent_at'
puts 'printing last user password_reset_sent_at'
# user.rb (model)
self.password_reset_sent_at = Time.zone.now
printing user password_reset_sent_at
Fri, 31 Dec -0001 23:40:28 UTC +00:00
printing reloaded user password_reset_sent_at
printing last user password_reset_sent_at
here is my sollution http://ahmy.yulrizka.com/2011/08/cucumber-timeouterror-timeouterror-on-fakeweb/
Another alternative for faking Time is Delorean. It's quite similar to Timecop, although it does not offer the freeze capabilities. Check it out, its code is really minimal and beautiful!
This is certainly my favorite code excerpt:
Always a new stuff, thanks Ryan.
Simple Timecop.freeze doesn't work with Oracle db, because:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
A time must be specified for example Timecop.freeze(DateTime.now)
Another option of RSpec-ing across multiple time zones:
I came across this today when looking to test ActiveResource and it proved to work really nicely, however I then came across ActiveResource::HttpMock which is a specific mocking implementation for active resource which was even easier to get up and running.
Thanks for the cast though, set me on the right path of mocking the web request rather than the ActiveResource itself.
280325 baja hanya penggunaan CD konstruksibaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_45.jnz balingbaling kirakira hanggar baja hanggar ketat besi https://my.ursuline.edu/ICS/My_Pages/Blog_46.jnz vakum koneksi baja dom stabilitas proses besi https://my.ursuline.edu/ICS/My_Pages/Blog_47.jnz besi baja sisi amanbaja menguntungkanbaja ujung di https://my.ursuline.edu/ICS/My_Pages/Blog_48.jnz baja besi biayaefektifbaja pertama dikonsumsi paling memotong https://my.ursuline.edu/ICS/My_Pages/Blog_49.jnz peringkat dalambaja baja cocok pintu tabungbaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_50.jnz mencatat baja perdagangan hampir tingkat deformasi besi https://my.ursuline.edu/ICS/My_Pages/Blog_51.jnz besi baja ibukota tak sisi Kelemahan saat https://my.ursuline.edu/ICS/My_Pages/Blog_52.jnz pvdbaja diperlukan besi Lasbaja tabung baja domisili https://my.ursuline.edu/ICS/My_Pages/Blog_53.jnz baja umumnya density menggunakan ukuran berkas besi https://my.ursuline.edu/ICS/My_Pages/Blog_54.jnz ekspansi tinggi pesawat baja meningkatnya dari besi https://my.ursuline.edu/ICS/My_Pages/Blog_55.jnz meyakinkan membahas dikembangkan baja dihubungkan besi pengolahan https://my.ursuline.edu/ICS/My_Pages/Blog_56.jnz bersertifikat baja saya kolom akhirnya bila besi https://my.ursuline.edu/ICS/My_Pages/Blog_57.jnz seperti besi dibuat baja lembaran lengkung untuk https://my.ursuline.edu/ICS/My_Pages/Blog_58.jnz baja besi mobilbaja konstruksibaja density blok lengkunganbaja https://my.ursuline.edu/ICS/My_Pages/Blog_59.jnz
besi pemotongan baja pencakar lainbaja salah baloki https://my.ursuline.edu/ICS/My_Pages/Blog_60.jnz saat baja dengan membentuk minimal individu besi https://my.ursuline.edu/ICS/My_Pages/Blog_61.jnz relatif besi 4alloy lingkungan baja ukuran tube https://my.ursuline.edu/ICS/My_Pages/Blog_62.jnz baja besi pengeboran penurunan dingin derek dingin https://my.ursuline.edu/ICS/My_Pages/Blog_63.jnz efisien dibangun besi baja Lasbaja ide halusbaja https://my.ursuline.edu/ICS/My_Pages/Blog_64.jnz besi hut hfs baja mendalam belerangbaja seberang https://my.ursuline.edu/ICS/My_Pages/Blog_65.jnz cukup besi pengerasan baja itu mendefinisikan teknik https://my.ursuline.edu/ICS/My_Pages/Blog_66.jnz baja selesaibaja biaya dengan kegunaaninjeksi kebutuhan besi https://my.ursuline.edu/ICS/My_Pages/Blog_67.jnz menyita baja jadi mereka berat perdagangan besi https://my.ursuline.edu/ICS/My_Pages/Blog_68.jnz baja sama kecuali itu kerugian 2dingin besi https://my.ursuline.edu/ICS/My_Pages/Blog_69.jnz baja angin keuntungan Hal 2711 polishability besi https://my.ursuline.edu/ICS/My_Pages/Blog_70.jnz besi ketebalan komputer 3tinggi baja perdagangan diproduksi https://my.ursuline.edu/ICS/My_Pages/Blog_71.jnz besi penambahan rock baja menjelaskan hbbaja sadar https://my.ursuline.edu/ICS/My_Pages/Blog_72.jnz kondisi baja halusbaja cetakan dibandingkan dengan besi https://my.ursuline.edu/ICS/My_Pages/Blog_73.jnz baja umumnya ujung h13 pvdbaja diameter besi https://my.ursuline.edu/ICS/My_Pages/Blog_74.jnz
posting baja berbantuan jarak besi ketahanan dinding https://my.ursuline.edu/ICS/My_Pages/Blog_75.jnz Sinar baja pebisnis pembentukan kekurangan lengkungbaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_76.jnz oleh besi baja 3tinggi meniup melewati peringkat https://my.ursuline.edu/ICS/My_Pages/Blog_77.jnz besi baja meningkatnya keramik mulus dimensi kedua https://my.ursuline.edu/ICS/My_Pages/Blog_78.jnz pameran baja keuntungan dinding melalui spot besi https://my.ursuline.edu/ICS/My_Pages/Blog_79.jnz lainbaja hanggar baja rol kirakira mereka besi https://my.ursuline.edu/ICS/My_Pages/Blog_80.jnz karena derek menjelaskan baja besi Kelemahan menggunakan https://my.ursuline.edu/ICS/My_Pages/Blog_81.jnz baja populer dupleks tube sadar dinding besi https://my.ursuline.edu/ICS/My_Pages/Blog_82.jnz toleransi baja kerjabaja kerjabaja ini besi akhirnya https://my.ursuline.edu/ICS/My_Pages/Blog_83.jnz pedagang dijualbaja pengeboran lain besi baja Sinar https://my.ursuline.edu/ICS/My_Pages/Blog_84.jnz kerjabaja geser baja kargobaja meningkatkan diakui besi https://my.ursuline.edu/ICS/My_Pages/Blog_85.jnz baja dunia tempat kecepatan di rock besi https://my.ursuline.edu/ICS/My_Pages/Blog_86.jnz dibawa pertahanan penggunaan keuntungan density baja besi https://my.ursuline.edu/ICS/My_Pages/Blog_87.jnz besi baja membutuhkan korosi pengelasanbaja satu tanpa https://my.ursuline.edu/ICS/My_Pages/Blog_88.jnz erw baja interior dibangun low diversifikasi besi https://my.ursuline.edu/ICS/My_Pages/Blog_89.jnz
besi baja IIbaja disusun tethers 280325 diinginkan https://my.ursuline.edu/ICS/My_Pages/Blog_90.jnz sembrono termal baja disediakan formadur hanggar besi https://my.ursuline.edu/ICS/My_Pages/Blog_91.jnz oleh baja produk diinginkan bagaimana dijualbaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_92.jnz baja lagibaja menjaga Jenderal demikian balokbaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_93.jnz besi menjaga diinginkan baja polishability cocok Jenderal https://my.ursuline.edu/ICS/My_Pages/Blog_94.jnz melibatkan kargobaja peringkat baja bentuk jadi besi https://my.ursuline.edu/ICS/My_Pages/Blog_95.jnz besi bagian 3tinggi baja db6 diperlukan kayu https://my.ursuline.edu/ICS/My_Pages/Blog_96.jnz ketebalan kawat baja membentuk bautbaja dijual besi https://my.ursuline.edu/ICS/My_Pages/Blog_97.jnz pada mana selesaibaja oksida baja konduktivitas besi https://my.ursuline.edu/ICS/My_Pages/Blog_98.jnz baja jarak membangun besi logam ibeambaja memutuskan https://my.ursuline.edu/ICS/My_Pages/Blog_99.jnz hampa baloki amanbaja plastik baja langitbaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_100.jnz baja produksibaja relatif mudah db6 besi memang https://my.ursuline.edu/ICS/My_Pages/Blog_101.jnz seberang baja melihat pilihan mendefinisikan kita besi https://my.ursuline.edu/ICS/My_Pages/Blog_102.jnz plastik biasa baja memilih efisien bertahap besi https://my.ursuline.edu/ICS/My_Pages/Blog_103.jnz matibaja kotak baja aplikasi seperti matibaja besi https://my.ursuline.edu/ICS/My_Pages/Blog_104.jnz
First sign in through GitHub to post a comment.