rspec on davidchua https://dchua.com/tags/rspec/ Recent content in rspec on davidchua Hugo -- gohugo.io en-us Wed, 24 Dec 2014 00:00:00 +0000 How to Test for File Operations with FakeFS and RSpec https://dchua.com/posts/2014-12-24-how-to-test-for-file-operations-with-fakefs-and-rspec/ Wed, 24 Dec 2014 00:00:00 +0000 https://dchua.com/posts/2014-12-24-how-to-test-for-file-operations-with-fakefs-and-rspec/ Background: Say you have controller method that creates and writes into a file and you’d like to be able to run expectations on the behavior and not so much of the implementation. The normal way would be to do something like: def test_creates_directory FileUtils.expects(:mkdir).with("directory").once Library.add "directory" end because this just doesn’t work. def test_creates_directory Library.add "directory" assert File.directory?("directory") end So one way to solve this is with this gem called FakeFS. DRY your ActionMailer::Base.deliveries.clear in RSpec tests https://dchua.com/posts/2014-08-12-dry-your-actionmailer-base.deliveries.clear-in-rspec-tests/ Tue, 12 Aug 2014 12:22:03 +0800 https://dchua.com/posts/2014-08-12-dry-your-actionmailer-base.deliveries.clear-in-rspec-tests/ When testing for mailers, you might need to add the following quite frequently: describe "test mailer" do it "should work" do ActionMailer::Base.deliveries.clear # bunch of other # codes here end end You probably would add them at the start of every test block that tests for ActionMailer. The neater way would be to do this: describe "test mailer", type: :mailer do it "should work" do # bunch of # codes here end end By assigning a mailer_type to the test describe, RSpec will automatically clear the actionmailer deliveries at the start.