rspec2

RunTimeError: ActionController::RackDelegation in rspec 2.10.1 for rails 3.1.4 application controller

前提是你 提交于 2019-11-29 14:00:29
问题 In our rails 3.1.4 app, rspec is used to test the public method require_signin in application controller. Here is the method require_signin: def require_signin if !signed_in? flash.now.alert = "Log in first!" redirect_to signin_path end end Here is the rspec code: it "should invoke require_signin for those without login" do controller.send(:require_signin) controller {should redirect_to signin_path} end The above rspec generates gigantic multi pages error starting like the below: RuntimeError

Rspec 2.7 access controller session in spec before making request

痴心易碎 提交于 2019-11-29 09:00:45
问题 I'm testing my controllers using Rspec and I can't seem to set the session variable of the current controller under test before making the request to the path. For example this works: describe "GET /controller/path" do it "if not matching CRSF should display message" do get controller_path request.session[:state] = "12334" end end This doesn't work (i get an error saying session is not a method of Nil class): describe "GET /controller/path" do it "if not matching CRSF should display message"

Factory Girl sequences not incrementing

可紊 提交于 2019-11-29 04:03:29
I'm trying to get FactoryGirl to generate some names for me, but the sequence doesn't seem to increment. # spec/factories/vessel.rb require 'factory_girl' FactoryGirl.define do sequence :vessel_name do |n| "TK42#{n}" end factory :vessel do name FactoryGirl.generate(:vessel_name) vessel_type 'fermenter' volume_scalar 100.0 volume_units 'bbl' end end # spec/models/vessel_spec.rb require 'spec_helper' describe Vessel do context 'working in the factory' do it 'makes a valid vessel' do vessel = FactoryGirl.create(:vessel) vessel.should be_valid, "Invalid vessel #{vessel.valid? || vessel.errors

Rspec: setting cookies in a helper test

不问归期 提交于 2019-11-29 03:04:56
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 SET cookies, so I'll leave it here for other's reference. the piece I was looking for: helper.request

Upgrading to devise 3.1 => getting Reset password token is invalid

橙三吉。 提交于 2019-11-29 00:55:44
Solution Thanks to this gist form Steven Harman, I got it working. devise_mail_helpers.rb module Features module MailHelpers def last_email ActionMailer::Base.deliveries[0] end # Can be used like: # extract_token_from_email(:reset_password) def extract_token_from_email(token_name) mail_body = last_email.body.to_s mail_body[/#{token_name.to_s}_token=([^"]+)/, 1] end end end I added the file devise_mail_helpers.rb to the same folder as the features specs and wrote this spec. require 'devise_mail_helpers.rb' include Features include MailHelpers describe "PasswordResets" do it "emails user when

How to test attr_accessible fields in RSpec

时光毁灭记忆、已成空白 提交于 2019-11-29 00:09:44
问题 So we have been setting up attr_accessible and attr_protected on many fields through out our Rails 3.2 app. For now we really don't test to ensure that these fields are protected. So I decided to google some answers and stumbled upon this solution: RSpec::Matchers.define :be_accessible do |attribute| match do |response| response.send("#{attribute}=", :foo) response.send("#{attribute}").eql? :foo end description { "be accessible :#{attribute}" } failure_message_for_should { ":#{attribute}

POSTing raw JSON data with Rails 3.2.11 and RSpec

可紊 提交于 2019-11-28 21:02:35
In order to ensure that my application is not vulnerable to this exploit , I am trying to create a controller test in RSpec to cover it. In order to do so, I need to be able to post raw JSON, but I haven't seemed to find a way to do that. In doing some research, I've determined that there at least used to be a way to do so using the RAW_POST_DATA header, but this doesn't seem to work anymore: it "should not be exploitable by using an integer token value" do request.env["CONTENT_TYPE"] = "application/json" request.env["RAW_POST_DATA"] = { token: 0 }.to_json post :reset_password end When I look

How to include Rails Helpers on RSpec

谁说我不能喝 提交于 2019-11-28 20:06:44
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 error: /spec/support/helpers.rb:2:in `<module:Helpers>': uninitialized constant Helpers::ActionView

How to get rspec-2 to give the full trace associated with a test failure?

帅比萌擦擦* 提交于 2019-11-28 17:20:57
Right now if I run my test suite using rake spec I get an error: 1) SegmentsController GET 'index' should work Failure/Error: get 'index' undefined method `locale' for # # ./spec/controllers/segments_controller_spec.rb:14: in `block (3 levels) in ' This is normal as I do have an error :) The problem is that the trace isn't very helpful. I know it broke in segments_controller_spec.rb , line 14, but this is just where I call the test: ### segments_controller_spec.rb:14 get 'index' I would prefer to have the actual line breaking and the complete trace, not the part in the spec folder. Running

Rails - RSpec - Difference between “let” and “let!”

孤人 提交于 2019-11-28 15:46:35
问题 I have read what the RSpec manual says about the difference, but some things are still confusing. Every other source, including "The RSpec Book" only explain about "let", and "The Rails 3 Way" is just as confusing as the manual. I understand that "let" is only evaluated when invoked, and keeps the same value within a scope. So it makes sense that in the first example in the manual the first test passes as the "let" is invoked only once, and the second test passes as it adds to the value of