Rails - Getting RSpec Working - ruby-on-rails

I'm learning how to do integration testin on my rails app. I started with cucumber but then learned since I'm just testing a models method that I should be using RSPEC. So now I'm trying to get RSPEC installed.
When I run autotest, it is only looking at the /features directory not the /spec directory.
Here's what I've done so far:
.autotest
require 'autotest/growl'
require 'autotest/fsevent'
Autotest.add_hook(:initialize) {|at|
at.add_exception %r{^\.git} # ignore Version Control System
at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
# at.clear_mappings # take out the default (test/test*rb)
at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
Dir['spec/**/*.rb']
}
nil
}
/spec - created directory
/spec/spec_helper.rb - created
/spec/lib/mailingjob_spec.rb - created, tried to write a case that would fail as follows:
require 'spec_helper'
describe User do
before(:each) do
#valid_attributes = {
:login => "akm",
:email => "akm2000#gmail.com",
:password => "D1ff1cultPa55w0rd",
:password_confirmation => "D1ff1cultPa55w0rd"
}
end
it "should create a new instance given valid attributes" do
User.create!(#valid_attributesXXXX)
end
end
But when I run autotest this rpsec is never run only the features dir with cucumber is running. Ideas? thanks

Try:
rspec --configure autotest
From the autotest README:
[RSpec] if you want to use require
'spec_helper' -> rspec --configure
autotest

Related

Rspec/Capybara "feature" method undefined when Guard runs specs in watched files, works when run manually

I am getting a strange issue when using Guard to run my specs.
I am running a feature spec that uses the Capybara "feature" / "scenario" syntax. I am also using Spring.
Everything works fine if I run rspec spec or spring rspec in the console or rspec in the Guard shell. But, when the watched specs get run automatically by Guard, I get the following error:
/spec/features/navigation_spec.rb:1:in <top (required)>': undefined methodfeature' for main:Object (NoMethodError)
Why is it not picking up the Capybara syntax only in this specific context?
Here is the relevant code:
GuardFile
guard :rspec, :spring => true do
watch(%r{^spec/.+_spec\.rb$})
end
spec/features/navigation_spec.rb
feature "navigation" do
context "When on the home page" do
before { visit "/" }
scenario "I should see the navigation header" do
expect(page).to have_selector("div.navigation")
end
end
end
spec/spec_helper.rb
require 'capybara/rspec'
For anyone who may run into a similar issue in the future, I forgot to include require 'spec_helper' in the feature spec (like an idiot).
In Rails 4, make sure that you have included 'rails_helper' instead of 'spec_helper' on top of your specfile:
require 'rails_helper'
feature "Some Feature", :type => :feature do
..
end
And also make sure that config.disable_monkey_patching! is commented out or removed. Otherwise you will encounter problems when running your feature specs.
require 'capybara/rspec'
RSpec.configure do |config|
..
# config.disable_monkey_patching!
..
end
If you have created a .rspec file inside your project dir, also make sure to to change spec_helper to rails_helper there as well.
How are you invoking guard? It sounds like you might need to do bundle exec guard to kick things off. It could also be running under the wrong environment (unlikely, but worth a look).

Include tag along with other tagless tests?

Right now I rspec configured so that running the rspec command by itself excludes any tests that require loading the Rails environment:
RSpec.configure do |config|
config.filter_run_excluding :type => 'feature'
end
The excluded tests look like the following:
describe 'Feature that requires rails', :type => :feature do
# test, test, test
end
The command rspec -t type:feature will run these tests exclusively.
With this configuration, is there a way to run all tests in one command, including the feature tests?
The way I accomplish this is to use an environment variable so changing what you have to:
RSpec.configure do |config|
config.filter_run_excluding :type => 'feature' unless ENV["ALLOW_FEATURES"]
end
and then running your tests with:
ALLOW_FEATURES=true rspec
will ignore the exclude and run all tests

Should I use Selenium or Jasmine in order to test view files with RSpec?

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2 with the Selenium driver. After receiving the answer to a my previous question I had a doubt: should I use the Selenium ruby-gem or the Jasmine ruby-gem in order to test view files with RSpec? If so, since I am already using Selenium in order to test JavaScript for view files with Cucumber, why (when testing with RSpec) should I use Jasmine instead of Selenium? That is, why to use two ruby-gems that have the same purpose and make the same things?
Generally and practically speaking, how do you advice to test view files by using RSpec? ... but, is it the "right way" to test view files with RSpec or should I test those by using Cucumber?
I prefer to use selenium server, the selenium IDE in firefox to record and selenium client for rspec.
selenium_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path(File.join(File.dirname(__FILE__),'../..','config','environment'))
require 'spec/autorun'
require 'spec/rails'
require 'factory_girl'
require 'rspec/instafail'
require "selenium/client"
Spec::Runner.configure do |config|
end
rspec_tester_file_spec.rb
require "selenium/selenium_helper"
require "selenium/modules/loginator"
describe "tester" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
#verification_errors = []
#selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://localhost:3000",
:timeout_in_second => 60
end
before(:each) do
#selenium_driver.start_new_browser_session
end
append_after(:each) do
#selenium_driver.close_current_browser_session
end
it "login and then try to search for stuff" do
#login.run_login(page)
page.click "link=CSR"
page.wait_for_page_to_load "30000"
page.type "id=isq_Name", "john smith"
page.click "css=input[type=\"submit\"]"
page.wait_for_page_to_load "30000"
page.is_text_present("Update")
page.is_text_present("Date")
page.is_text_present("PIN")
page.is_text_present("Name")
end

Factory_Girl rake issue

I'm creating a new Rails 3.1 application using Cucumber, Devise, and Factory_Girl. I installed successfully Devise and Cucumber. I then created the spec/factories.rb file that contains the following:
require 'factory_girl'
Factory.define :user do |user|
user.name 'Test User'
user.email 'test#email.com'
user.password' testpass'
end
I also added spec/support/devise.rb to add in the test hooks for Devise:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
In regards to testing, I have not touched anything else and now everytime I run any rake command I get the following (I ran rake spec in this case, just to make sure everything is ready for tests):
rake aborted!
Factory already registered: user
Tasks: TOP => spec => db:test:prepare => db:abort_if_pending_migrations => environment
(See full trace by running task with --trace)
Now if I remove the Factory.define block from the factories file, it runs fine. I've done some Google searching and have come up with absolutely nothing. Since it's saying the Factory is already registered, is Devise for whatever reason, creating a factory already?

Starting rails before rspec tests?

Is there any way within an RSpec tests, by convention or code, to have rails start before tests run? I'm trying to setup a testing framework for selenium tests that use chrome, and now I'm only hindered by my lack of a running server.
require 'spec_helper'
describe 'The first tab' do
before(:each) do
#driver = Selenium::WebDriver.for :chrome
end
it 'Shows the list' do
#driver.navigate.to 'index.html'
end
end
I'm new to RSpec, so I'm not sure how I would create a suite of tests that all ran while a rails server was running.
You should be using Capybara to test this stuff instead. It uses selenium-webdriver internally to provide JavaScript testing support.
with Capybara, you put this test in either the spec/integration or spec/requests folder and write it like this:
require 'spec_helper'
describe 'The first tab' do
it "shows the list", :js => true do
visit list_path
end
end
By putting :js => true after the example's name Capybara will know to run this as a JavaScript test.
Just run rails server and kill the process went tests complete.

Resources