rspec2

ControllerMacros cannot be seen in RSpec

百般思念 提交于 2019-11-28 12:10:17
I have a Rails app and I am trying to test it. I use devise to log in. However, I faced a problem that occurs when I want to test: Users/ender/Projects/ratingw/spec/controllers/widgets_controller_spec.rb:4:in `block in <top (required)>': undefined local variable or method `login_user' for #<Class:0x007fe909bd5070> (NameError) First, I want to say that I read this devise formal tutorial . My spec_helper.rb is: # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails'

How to test routes in a Rails 3.1 mountable engine

有些话、适合烂在心里 提交于 2019-11-28 09:02:04
I am trying to write some routing specs for a mountable rails 3.1 engine. I have working model and controller specs, but I cannot figure out how to specify routes. For a sample engine, 'testy', every approach I try ends with the same error: ActionController::RoutingError: No route matches "/testy" I've tried both Rspec and Test::Unit syntax (spec/routing/index_routing_spec.rb): describe "test controller routing" do it "Routs the root to the test controller's index action" do { :get => '/testy/' }.should route_to(:controller => 'test', :action => 'index') end it "tries the same thing using Test

Testing STDOUT output in Rspec

放肆的年华 提交于 2019-11-28 05:15:27
I am trying to build a spec for this statement. It is easy with 'puts' print "'#{@file}' doesn't exist: Create Empty File (y/n)?" RSpec 3.0+ RSpec 3.0 added a new output matcher for this purpose: expect { my_method }.to output("my message").to_stdout expect { my_method }.to output("my error").to_stderr Minitest Minitest also has something called capture_io : out, err = capture_io do my_method end assert_equals "my message", out assert_equals "my error", err RSpec < 3.0 (and others) For RSpec < 3.0 and other frameworks, you can use the following helper. This will allow you to capture whatever

Using implicit `subject` with `expect` in RSpec-2.11

被刻印的时光 ゝ 提交于 2019-11-28 05:14:49
With the new expect syntax in rspec-2.11, how is it possible to use the implicit subject ? Is there a better way than explicitly referencing subject , like below? describe User do it 'is valid' do expect(subject).to be_valid # <<< can `subject` be implicit? end end If you configure RSpec to disable the should syntax, you can still use the old one-liner syntax, since that doesn't involve should being added to every object: describe User do it { should be_valid } end We briefly discussed an alternate one-liner syntax, but decided against it since it wasn't needed and we felt like it might add

Turn off transactional fixtures for one spec with RSpec 2

人走茶凉 提交于 2019-11-28 04:54:35
How do I turn off transactional fixtures for only one spec (or Steak scenario) with RSpec 2? I tried some things found on the web without any success. This leads to an undefined method exception. describe "MyClass without transactional fixtures" do self.use_transactional_fixtures = false ... end This simply does nothing (transactional fixture is still on): describe "MyClass without transactional fixtures" do RSpec.configure do |config| config.use_transactional_fixtures = false end ... end What else could I try? This used to be a bug (see ticket #197 ), but I seems to be okay now. I just don't

Hide the list of files when running rspec?

柔情痞子 提交于 2019-11-28 03:39:20
问题 When running rake spec on the command line for a large Rails project, I get a giant list of every rspec file that will be run. Is there a way to hide that by default? ruby-1.9.3-p448/bin/ruby -S rspec ./spec/acceptance/replicators/activity_replicator_spec.rb ./spec/acceptance/replicators/template_replicator_spec.rb ./spec/authorization_rules/admin_authorization_rules_spec.rb ... When I run just rspec (no rake call) I don't get this console output. EDIT 1 Working from phoet's answer, I tried

Setup RSpec to test a gem (not Rails)

守給你的承諾、 提交于 2019-11-28 02:33:26
It is pretty easy with the added generator of rspec-rails to setup RSpec for testing a Rails application. But how about adding RSpec for testing a gem in development? I am not using jeweler or such tools. I just used Bundler ( bundle gem my_gem ) to setup the structure for the new gem and edit the *.gemspec manually. I also added s.add_development_dependency "rspec", ">= 2.0.0" to gemspec and did a bundle install . Is there some nice tutorial what to do next to get RSpec working? iain I've updated this answer to match current best practices: Bundler supports gem development perfectly. If you

How do I prepare test database(s) for Rails rspec tests without running rake spec?

吃可爱长大的小学妹 提交于 2019-11-27 17:39:31
After significant troubleshooting, I figured out that I needed to run rake spec once (I can abort with control-c) before I can run rspec directly (e.g. on a subset of our specs). We are running Rails 3.0.7 and RSpec 2.5.0. Clearly, rake is running some important database setup tasks / code (we have custom code in the root level rails Rakefile and possibly other places). How can I run the rake test database setup tasks / code without running rake spec ? In addition to being able to run rspec on a subset of files, I am using specjour to spread our specs across multiple cores (haven't had success

Rspec: setting cookies in a helper test

末鹿安然 提交于 2019-11-27 17:20:55
问题 Helper Method # Determine if this is user's first time def first_time? cookies[:first_time].nil? end Attempted Rspec test it "returns true if the cookie is set" do cookies[:first_time] = "something" helper.first_time?().should be(true) end Error: undefined method `cookies' for nil:NilClass Everything I've read about Rspec and cookies has to do with the controller. Any way to get/set cookies in Rspec helper tests? (Rspec/Rspec-rails 2.5, Rails 3.0.4) Thanks!! UPDATE: Found an answer on how to

How to include Rails Helpers on RSpec

不想你离开。 提交于 2019-11-27 11:29:22
问题 I'm trying to include some helpers to test with rspec but no luck. What I did: created a support/helpers.rb file under my spec folder. support/helpers.rb module Helpers include ActionView::Helpers::NumberHelper include ActionView::Helpers::TextHelper end and tried to require this file in spec_helper.rb . # This file is copied to spec/ when you run 'rails generate rspec:install' require 'rubygems' require 'spork' require 'support/helpers' Spork.prefork do . . end this generates the following