Rails 3.1 Engine plugin rspec testing (external gem Spree Commerce) - ruby-on-rails-3.1

I created an engine inside an app that depends on an external gem: Spree.
And I did my best to set RSpec within that engine following this guide.
When I try testing a controller with some integrated testing:
tests_spec.rb:
require 'spec_helper'
describe "Tests" do
describe "GET /tests" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get tests_path
response.status.should be(200)
end
end
end
I get an error in the dummy app's environment.rb file, that says that following command couldn't be run:
# Initialize the rails application
Dummy::Application.initialize!
Because my controller is using Admin::ResourceController as an extension which is declared within the external Spree gem. As if rspec is unable to read that class from gemfile of the main app (the one that contains the engine plugin).
Can someone please shed some light on that?

Related

RSpec/Capybara without Rails Setup for Page Objects/Support Files?

So im going to be writing a framework that uses Rspec/Capybara and Selenium to do some automation testing on an app. Im using purely RSpec (no rails) and im having some difficulties in getting everything setup correctly.
The problem I am running into is when im trying to include page object files into my specs. Right now my directory looks like:
spec (Actual .spec files go here, spec_helper is also here)
spec/support (Page Object Files go here)
In my page object files my first problem is it can't find the Capybara DSL unless I do: include Capybara::DSL at the top of each page object file (Which apparently isn't a good idea according to the warning message it gives me)
The other problem is that including my page object files in my tests Im needing to do a require_relative to the specific file (Which is sort of a pain) otherwise it can't find the class.
Is there something in spec_helper that will fix this? Im not an expert in ruby so I assume Im missing something. The Capybara DSL problem I can't figure out either for example requiring require 'capybara/rspec' doesn't seem to help either.
You have a few options for Capybara::DSL -
Include Capybara::DSL into whatever base class you're using for your PageObjects so the Capybara methods are available in that class (as opposed to on the global object) - ie. the include Capybara::DSL goes inside the class definition - not at the top of the file.
Keep an instance of Capybara::Session in your page object instances and call all capybara methods on that session instance - #my_session.find(...), etc.
Always use Capybara.current_session when calling Capybara methods - Capybara.current_session.find(...), etc
As for the require issue -- you can either use require_relative to specify files to require based from the current file, or you use require and specify the location from the projects root (current working directory). If you have a directory of files you want loaded you can add something like the following to your spec_helper.rb to tell it to load all the files
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }

Using `wicked_pdf` in a non-rails project

All the documentation for wicked_pdf assume you are using rails.
But I want to use this feature in a non-rails environment (a Jekyll plugin, written in Ruby).
Trying to use pdf = WickedPdf.new.pdf_from_string(page.content) returns Error: uninitialized constant WickedPdf.
How do I initialize WickedPdf without the rails generate wicked_pdf provided in their documentation?
I had to add gem 'activesupport' to my gemfile, then use require 'wicked_pdf' in my source code.
wicked_pdf depends on active_support/core_ext - as you can see in lib/wicked_pdf.rb. However you can use it outside of controller "context" like this...
pdf = WickedPdf.new.pdf_from_string('<h1>My life is wicked.</h1>')

Models on the Rails Console

I have created a simple class under the models folder like so:
class CaffeineShops
def initialize
end
end
When I run rails console and try CaffeineShops.new(), I get a message: "NoMethodError: undefined method 'new' for CaffeineShops::Module"
I am using Rails 4.1.
Any ideas why I am getting that error?
You cannot name a model the same name as the application itself. When you run rails g caffeine_shops this creates a module named CaffeineShops which will power your application.
The module is utilized in many files including
config\application.rb
config\environment.rb
config\environments\development.rb
config\environments\production.rb
config\environments\test.rb
config\initializers\secret_token.rb
config\initializers\session_store.rb
config\routes.rb
config.ru *actually starts the application on Rack-based servers
Rakefile *loads rake tasks for the application
When you then try to name a class the same name it creates ambiguity in the rails application so your code is assuming you mean the module CaffeineShops not the class CaffeineShops.
If you were to actually use a generator to define this model rails would make it very clear there was a problem with this e.g.
rails g caffeine_shops
cd caffeine_shops
rails g model caffeine_shops
#=>The name 'CaffeineShops' is either already used in your application or reserved
by Ruby on Rails. Please choose an alternative and run this generator again.

How do you set up Rack::URLMap to work with RSpec in Sinatra?

I'm relatively new to Sinatra, and I want to figure out a way to integrate RSpec with my Sinatra setup.
config.ru
require 'sinatra'
require 'mongoid'
require 'uri'
require './lib/twilio_wrapper'
Mongoid.load!("./config/mongoid.yml")
Dir["./controllers/*.rb"].each { |file| require file }
run Rack::URLMap.new(
'/' => HomeController.new,
'/users' => UsersController.new(TwilioWrapper.new)
)
With this setup, I can modularize my controllers and create single instances of helper classes (such as TwilioWrapper). However, if I want to set up RSpec, I need to point it to my application's class. However, in the situation above, because I'm using Rack::URLMap, I don't have a specific application class to point RSpec to.
How can I keep my code modular in the fashion outlined above while including RSpec for tests?
Rack does not care about controllers, it cares about apps. So HomeController and UsersController are 2 Sinatra applications "racked up" in Rack. These are not controllers, they are separate Rack apps. I do not think you want 2 applications but rather to put these 2 controllers in 2 files so you can spec them out separately and keep the code readable.
The naming convention for Sinatra is to name it something like MyApp to reflect this. Sinatra is a flat framework, but you can name your "controller" files what you want.
So in folder routes you can have 'users.rb' and 'home.rb' but both files have at the top
MyApp < Sinatra::Application
Then you can test using Rack::Test with Rspec.
If you do indeed want to test 2 apps and want the prefix using Rack::Test w Rspec you simply need to define app in your spec_helper or spec file as:
def app
run Rack::URLMap.new(
'/' => HomeController.new,
'/users' => UsersController.new(TwilioWrapper.new)
)
end
All Rack::Test does is rackup your Sinatra app into a test container.
Also please see Phrogz's excellent answer on how to lay out a Sinatra application

Tutorials to Testing using RSPEC on PADRINO framework on RUBY

I am new to Ruby and have been asked to use it in our new project. We have also been asked to use Padrino (Sinatra) as backend/framework. For testing we have been asked to use Rspec. I have been hunting for tutorials for long that would guide in using Rspec for Ruby on Padrino. What I get is mostly with reference to RoR. But, I am in need of Ruby on Padrino.
Please guide me for the same with any starters/guides/references/discussions, etc.
Please correct me, if I am wrong anywhere. May be I haven't searched with the right combination of words/phrases for my issue.
I am using Ruby 1.9.3 and Padrino v.0.10.6.
Note : I have also referred the SO question, but it didn't help.
I never used Padrino, but it seems that it isn't much different from Sinatra.
I suggest reading Sinatra and RSpec resources.
You can get started with this:
How to use rspec in a Sinatra Application
Sinatra, RSpec and DataMapper: Configuring and using a database for tests
And by reading specs that were written by other people on GitHub. These are some of mine, but they are not the cleanest thing that exists.
EDIT: a short tutorial
Getting started with this framework is much quicker and easier than with Sinatra. :)
Install Padrino: gem install padrino
Create an application: padrino g project myapp -d datamapper -t rspec
The command speaks for itself. :)
Run the tests: rspec --color
No tests were found, obviously. Let's create one in spec/hello/hello_spec.rb:
require File.dirname(__FILE__) + "/../spec_helper.rb"
describe "get '/'" do
it "should display hello world" do
get '/'
last_response.body.should == "Hello world!"
end
end
Run the tests again.
They failed, because no route get '/' exists. Let's create it.
In app/controllers/hello.rb:
Myapp.controller do
get '/' do
"Hello world!"
end
end
Run the test: it passes!
Check Padrino's documentation for more information and cool features, such as the controllers generator and the admin interface.
Good luck!

Resources