How do I test ActionMailer? - ruby-on-rails

I am using ActionMailer for the first time. It becomes a chore having to go into the development.log to and check if they are going, and I would like to write an automated test. What is the best/most practical solution for testing ActionMailer emails on Rails?

You can try the email-spec gem. It's really good and you can test a lot of stuff with it.

Related

Open source Rails apps that use fixtures

I'm working on an existing app with a test suite based on vanilla Rails fixtures and MiniTest. Switching to FactoryGirl or the like is not an option.
I'd like to find a relatively complex open source Rails app to learn more about best practices with using fixtures. Any suggestions?
Take a look at https://github.com/Shopify/active_merchant, it also uses some fixtures
I've discovered Faker for myself, what is important:
It creates unique data every time while seeding database.
In some cases it does matter and better than same data. So, it is suitable for fixtures only;-)
It has handy templates for most cases e.g. phone.
You can check example in GitLab repo.

What's the benefit of using mailman gem over ActionMailer?

I'm trying to understand what the benefit is here; the only I can see is avoiding a rails bootstrap every time an email is received, but I imagine this is possible with ActionMailer as well with a bit of set up. Any help would be very useful.
Chris.
http://rubygems.org/gems/mailman
The benefit is to avoid the Rails overhead, and it provides some syntax sugar. Also to avoid having to write it yourself.
The fact is though that the underpinnings of the Mailman Micro-framework are essentially the same as ActionMailer the Mail gem. You can achieve the same results using ActionMailer or just Mail without the added dependencies. It just really depends on your use case, and how much you want to write yourself.

What are spec/requests good for?

I use RSpec to test my lovely little web app. For integration tests I use Steak. When using Rails generators (yep, I know that this is not the Zen way of doing TDD) there are also some files in spec/requests generated. As stated on link text it is something similiar to integration test (but I couldn't find much more info).
Are those request specs still recommended when using something like Steak and Cucumber?
It all depends on what you need and want. The goal of testing is to prove that your app works once, not twice or more times.
I personally write rspec tests for models and helpers. I use cucumber to test that my views and controllers are working the way I expect them to. With this I can prove that my entire app works as I expect it to, so no, I don't use spec/requests.
Occasionally I do use spec/requests to test APIs, but you can do that with cucumber as well.
Some don't like the BDD-way cucumber works and stick with spec/requests. In the end it's all a matter of taste.

How do you develop outside-in Rails app using Cucumber & RSpec?

I just get started using BDD in Rails application, but I'm not sure what are best practices and workflows? And what other things that I really need for testing for my project such as step definitions, controllers, models, and views? Do I need to test all of those?
I generally think of Cucumber as a way to do integration testing on your application. Combined with Webrat, you can test user workflows, views and so on in a great way. For unit tests, you'll want to go down to a lower level and test your models just with rspec. You may also want to do some functional tests on the controllers, and I probably wouldn't use Cucumber for that either.
Here are a couple of videos:
http://confreaks.com/videos/72-mwrc2009-bdd-with-cucumber
http://rubyconf2008.confreaks.com/rspec-and-cucumber.html
Ryan Bates has some good Railscasts on these topics:
Beginning with Cucumber
Webrat
More on Cucumber
This may be a matter of taste, but having tried out Rspec I prefer using the built-in Rails testing framework along with a gem called Shoulda. In my opinion, that combination lets you write much clearer, more succinct and understandable tests than Rspec by far. But not everyone would agree.
Shoulda's contexts let you organize your tests into logical hierarchies which really helps when you're trying to test all the possible paths some crazy, branching situation, like user logs in with right pw, wrong pw, right pw but registration not confirmed, etc.
In addition be sure to install the ZenTest gem. That lets you just execute the command $ autotest and your tests will run automatically every time you change a file.

Simplest way to send mail with Ruby on Rails

What is the simplest way to send mail using Ruby on Rails? Is there a way to send mail directly via ruby and skip all the rails models and complexity, just like php's mail() function?
Thanks for your help.
The simplest way in plain old ruby is to use net/smtp. However rails has it's own built in mailing facilities, because sending mail is something that is pretty common. The best way to do it in rails, is to use a Mailer model
Make sure you replace all the example.com's with real values:
require 'net/smtp'
Net::SMTP.start('smtp.example.com', 25) do |smtp|
smtp.send_message "Subject: testing from ruby", 'from-address#example.com', ['to-address1#example.com', 'to-address2#example.com']
end
There's also TMail.
Another excellent solution is a gem called pony. It is exactly like php's mail() function. Simply and easy.
yes check out the ruby docs...http://ruby-doc.org/stdlib/
the package you want to look at is net/smtp
there is also
http://www.rfc20.org/rubymail/(ruby mail)
which is popular and make it a little easier
I'm concerned that if you don't want to use ActionMailer that maybe you just don't get rails. ActionMailer makes sending email with good templating and the like very very easy, you really should look into it.

Resources