contextio-ruby icon indicating copy to clipboard operation
contextio-ruby copied to clipboard

RSpec Helpers

Open chrishale opened this issue 11 years ago • 0 comments

I've been building a Rails app that uses this library and the Context.io service, and I think it would be really useful to have some helpers in the Gem for using in RSpec.

What I'm currently using

To provide some context of what I'm imagining for this enhancement I'll start by showing you what I'm currently using.

In my implementation I use some instance methods in my User model to connect to context.io;

def c
    @c ||= ContextIO.new(ENV['CONTEXT_APP'], ENV['CONTEXT_SECRET'])
end
def contextio_account
    @contextio_account ||= contextio_account_id.present?  c.accounts[contextio_account_id] : nil
end
def messages(options = {})
    contextio_account.messages.where(options) unless contextio_account.nil?
end

So if I call something like User.find(x).messages(folder: "Inbox") in a controller, I stub the messages method in a before block for each of my request specs;

before(:each) do
    User.any_instance.stub(:messages).and_return(mock_messages(5))
end

mock_messages is a method I've written in my spec helper, which currently looks like this;

def mock_messages(num = 1)
  messages = []
  num.times do |i|
    messages << Hashie::Mash.new({
      message_id: "5238bea48debea2a7f000002",
      email_message_id: "<something>@strobedigital.com",
      person_info: [{ "[email protected]" => {}, "[email protected]" => {} }],
      subject: "Message Subject #{i}",
      from: { 'name' => 'Chris', 'email' => '[email protected]'},
      date: Time.now.to_i,
      recieved_at: Time.now,
      body_parts: [
        { content: "Message content" },
        { content: "Message content" }
      ]
    })
  end
  messages
end

It uses the the Hashie gem, to make a Mash. Which allows me to mimic the attributes/methods that are available from the contextio-ruby gem.

This has been enough for me to do basic and quick request specs like this, without having to interact with any real data (using Capybara);

it "displays message subject" do
    visit '/'
    expect(page).to have_content "Message Subject"
end

Ideas?

I'm not too sure what would be the best option here, but some thoughts would be:

Maybe we could be use a yml file in the fixtures directory, where you mock up one or more fake email accounts. Perhaps provide a made up username, password, mailserver as well as the messages within the mailbox. Perhaps something like this.

Then you could load a fixture in a before block like:

ContextIO.config.mock_mailbox = :user1

And it would parse the yml file and stub all the relevant methods with information from the yml file. If there is any attributes missing from the yml file then it would just load in valid fake data. Things like dates, message id's etc. Which might not be important for some specs.

Any way, just some ramblings that I thought would be useful for RSpec nerds like me :)

chrishale avatar Sep 27 '13 09:09 chrishale