How should I test Mailman app - ruby

I'm trying to write some specs for my application aimed to process and resend incoming emails. It's built with Mailman App. I didn't find any good examples on how to do that. What am I actually trying is to create email (with Mail gem) and process it with Mailman. But there is only option of using stding for testing.
So, are there any examples of specs written to test the mailman apps?

I think it is useful to extract as much as possible out of your Mailman::Application block and put it into your app somewhere. Then test that processing.
This Railscast (pro-only, sorry) has an example of "An Alternative to Routing" that is pretty good.
Basically instead of doing something like:
Mailman::Application.run do
subject(/Update (\d+)/) do |ticket_id|
...
you just say:
Mailman::Application.run do
default do
MailProcessor.receive_mail(message)
end
end
Then the MailProcessor class would handle reading the message and calling the right other functions in your app (depending on the message's subject, recipient, sender, etc.) I would then RSpec test the MailProcessor class for doing the right thing.
This is similar to how I approach testing rake tasks (put the functionality in a lib file that you test, and have the rake task just call the lib file.) Then you can be fairly certain that the functionality is being tested properly. If you were paranoid about the rake task, you can try an approach like: http://robots.thoughtbot.com/post/11957424161/test-rake-tasks-like-a-boss.

Related

Annoying Guard notification when testing

Recently I made a simple ruby application and have been using minitest to test it.
Following the advice of the Head First Ruby book, I automated this testing using Rake(I'll write what it told me to put in the Rakefile at the end of this post, in case that helps). The test seems to run fine (everything passes in a way I would expect it to), but I always get this notification at the end of it all:
rvm/gems/ruby-2.3.0/gems/guard-2.14.0/lib/guard/notifier.rb:28: warning: instance variable #notifier not initialized
Testing things manually by telling ruby to include which files I want, does not have this issue, only when I use "rake test" to test things.
As far as I can tell, this is related to when I set up Guard when I was following Michael Hartl's Rails Tutorial, at the end of chapter 3. I followed the directions for setting that up (correctly, as far as I can tell), and this was all in a completely different folder(ultimately my ruby and rails projects do have the same parent folder that they sit in, but are themselves in completely separate ruby_projects and rails_projects folders). If possible, I would like to stop this notification on my ruby application that I am testing. Is there a good way to do this?
Contents of the Rakefile I am using, if that helps:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "lib"
t.test_files=FileList['test/**/test_*.rb']
end
My test file requires minitest/autorun, and the file for the application that I am testing, then has the normal tests
Seems like there's some weird conflict...
The reason is that Guard::Notifier.connect isn't connected. Normally, when you run guard, Guard.setup is called which does this.
If you're not using guard (e.g. interactively), then calling the following from your Rakefile should work around the problem:
Guard::Notifier.connect(notify: false, silent: true)
Guard::Notifier.disconnect
This will initialize the variable.
For a faster response, always report such issues on the project page on Github. If you can share the project where this occurs, maybe a better fix is possible. (It's best to provide a repository, since it really speeds up fixing things and often errors like this are very hard to simulate without the exact code).

Launch sinatra from a test/spec or another ruby script

I'm experimenting, and I'm trying to launch dummy Sinatra application from RSpec and kill it when the spec is finished. Something like:
# spec/some_spec.rb
before(:all)
# launch sinatra dummy app
end
after (:all)
# kill sinatra dummy app
end
it 'should return list of whatever' do
expect(JSON.parse(make_request('0.0.0.0:4567/test.json')))
.to include('whatever')
end
I could use system("ruby test/dummy/dummy_app.rb"), but how can I kill that process only? Does anyone know how I can launch the Sinatra inside a test (or from another ruby script)? I know about WebMocks, but I want to see if I can manage to make my test work this way.
Look under RSpec on "Testing Sinatra with Rack::Test". I'd suggest you use that code as boilerplate to get started.
Just add this to your describe block:
def app
Sinatra::Application
end
I would suggest you read up RSpec.
Since you want to test an external system, by the looks of your comment, instead of system "curl whatewer.com", you can use Net::HTTP to make requests and then test against the response.
Have a look at "Testing an external API using RSpec's request specs".
As I'm writing request specs to ensure the features won't be broken I decided to rather write separate Cucumber features. The nice thing is that I can use Capybara, and thanks to Selenium Web Drive, I can launch a server before I run my tests.
So, I created a dummy Sinatra application (that will represent the external service to which the actual code I'm testing is doing requests (including a nasty system('curl whatever.com')).
All I have to do is stub out the methods passed to curl to use Capybara.current_session.server.host and Capybara.current_session.server.port.
Once I'm done with my re-factoring all I have to do is remove the Capybara server variables, and Selenium web drive from the cucumber/capybara configuration.
Tests after a brief change will be still working and will be valid.
Update
In the end I wrote it all with RSpec request tests, as doing it in Cucumber was little bit time consuming and I already spend too much time on this.
I mark these kind of request tests with RSpec tag and Before I lunch these I manually lunch simple Sinatra/Grape dummy API application to which the request are made. (Then I run RSpec tests with this tag)
So basically I end up with specs for functionality that uses net/http that uses WebMock and don't need a server, and request tests for which I need to run the server before I run the specs. So the original question remains, how to lunch a server before tests start
After I cover all the functionality I'm gonig to rewrite the curl to net/http however I'm going to keep those requests specs as I discovered they are nice idea when it comes to crazy API scenarios (like testing https + diggested authentication)

jRuby Test Automation: Checking to see that an email has been sent. What libraries are available?

Background
I'm doing jRuby tests for a Grails app that sends emails. These tests are replacing tests that are normally done by manual testers. They check their inboxes to see that mail has been delivered.
Problem Statement
I want to do a similarly strong test that ensures the email is sent properly by the app.
I do not need any actual code to help do this necessarily, but it would help to know what technologies to use.
Why I've Been Stumped So Far
I've looked for solutions in the Ruby domain, but everybody that I've seen address this problem seems to be using Rails. I am not using action_mailer or pony (we have a grails app) so I can't use email_spec. If I could that would be great.
What can I do?
You can use the Rails approach to testing mails: monkey-patch mail send code during test setup to place all mail instances that should have been delivered into a globally accessible array, this way you can do asserts on that array.
Don't ask me for exact implementation details because I'm not into grails, but the general principle is this:
if you have a class MyMailThingy which has a deliver method on it's instance which performs the mail delivery (presumably mail subject, body etc are attributes on this object), then monkey patch:
class MyMailThingy
# same as `cattr_accessor :mock_queue` under rails
def self.mock_queue; ##mock_queue; end
def self.mock_queue=(arg); ##mock_queue=arg; end
def deliver
self.class.mock_queue ||= []
self.class.mock_queue << self.dup
self
end
end
and then in your tests you can assert on MyMailThingy.mock_queue how many mails have been sent, assert on their bodies, subjects, to/from fields etc without sending actual mails
I found an answer on how to send the mail here. It seems like Ruby has built in support for SMTP. I am looking at how to check the mail now.

Open mail client from cli ruby script like a mailto link

I have written a ruby cli script which takes a CSV and generates a PDF report, based on said CSV. I'm fairly new to Ruby, so while it's probably not the greatest code, I'm pretty proud of what I've made.
At any rate, what I would really like to do now, is make my script email said PDF as an attachment. I'm sure there is a library that understands SMTP and can send this on my behalf, but I would like to modify the email body, and review the attachments before sending. So it seems like the simplest thing would be to have the script start a new email in my system default mail client, providing the recipient, subject, and boiler plate text, and attaching the generated file, kind of like a mailto: link in a web page (does mailto support attachments?).
Seems like there could be a system command that does this, completely unrelated to Ruby, which I could have my Ruby script call. That would be fine. If it's platform dependent, I'm on OSX, but I move around, so am interested in Windows and Linux solutions, too.
I guess plan B would be a way to jam a simple CLI editor into my Ruby script, to let me edit the email text, and then use an SMTP library to send the email. That seems harder, unless it's already been done.
Actually you can execute any ruby file from command-line interface (CLI) using console_runner gem. If you already have written code you can run it from command line. All you need is to add annotations (YARD-like syntax) to your Ruby code and then execute it from command line:
$ c_run /path/your_file.rb say_hello
/path/your_file.rb:
# #runnable
class MyClass
# #runnable
def say_hello
puts 'Hello!'
end
end

How do I test a Curl based FaceBook API implementation?

I wrote my own FaceBook library that uses actual Curl requests, not libcurl.
Is there a way to test it? I'm asking this because most solutions involve using something like fakeweb which as far as I can tell will not work here.
The existing code can be found on my github page.
One approach would be to use a different host/port in test mode (eg localhost:12345)
Then in your test run a sinatra or webrick servlet on that port that you configure to respond to the requests your code should be making
You could mock Request.dispatcher with an expected behavior, pretty much like Fakeweb would do.
There are a few examples on this file, specially https://github.com/chrisk/fakeweb/blob/master/lib/fake_web/ext/net_http.rb#L44.
When running your tests/specs, monkey-patch the run method of your Request class to hook into the Marston VCR library. See the existing library_hooks subdir for examples and ideas on how to do this -- the fakeweb implementation is a good place to start.
VCR works well with live services like Facebook's because it captures interactions "as is", and VCRs can be easily re-recorded when the services change.
I'm running into problems with your library, however. You need to require the cgi and json libraries; it also looks like it requires a Rails environment (it's failing to find with_indifferent_access on Hash).

Resources