rspec2

Unable to stub helper method with rspec

故事扮演 提交于 2019-11-30 07:24:32
问题 I am trying to stub a method on a helper that is defined in my controller. For example: class ApplicationController < ActionController::Base def current_user @current_user ||= authenticated_user_method end helper_method :current_user end module SomeHelper def do_something current_user.call_a_method end end In my Rspec: describe SomeHelper it "why cant i stub a helper method?!" do helper.stub!(:current_user).and_return(@user) helper.respond_to?(:current_user).should be_true # Fails helper.do

Rspec 2.7 access controller session in spec before making request

梦想与她 提交于 2019-11-30 07:13:11
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" do request.session[:state] = "12334" get controller_path end end Any ideas? With new version of RSpec

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

。_饼干妹妹 提交于 2019-11-30 07:02:43
问题 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

Rails / RSpec: How to test #initialize method?

送分小仙女□ 提交于 2019-11-30 04:52:06
How can I specify #initialize behaviour with RSpec? For example here: generator.rb class Generator attr_accessor :seed def initialize(seed = nil) @seed = seed || pick_seed end def pick_seed Time.now.to_i end end generator_spec.rb require 'generator' describe Generator it "calls 'pick_seed' method unless seed specified" do end end I'd like to set expectation that pick_seed method called from #initialize method. Sean DeNigris For me, expectations are about designing conversations among collaborators. So, you have to decide - is #pick_seed an internal implementation detail or part of a

Resetting a singleton instance in Ruby

十年热恋 提交于 2019-11-30 04:46:37
How do I reset a singleton object in Ruby? I know that one'd never want to do this in real code but what about unit tests? Here's what I am trying to do in an RSpec test - describe MySingleton, "#not_initialised" do it "raises an exception" do expect {MySingleton.get_something}.to raise_error(RuntimeError) end end It fails because one of my previous tests initialises the singleton object. I have tried following Ian White's advice from this link which essentially monkey patches Singleton to provide a reset_instance method but I get an undefined method 'reset_instance' exception. require

“Could not find a valid mapping for #<User …>” only on second and successive tests

蓝咒 提交于 2019-11-30 04:29:55
I'm trying to write a request test that asserts that the proper links appear on the application layout depending in whether a user is logged in or out. FWIW, I'm using Devise for the authentication piece. Here's my spec: require 'spec_helper' require 'devise/test_helpers' describe "Layout Links" do context "the home page" do context "session controls" do context "for an authenticated user" do before do # I know these should all operate in isolation, but I # want to make sure the user is explicitly logged out visit destroy_user_session_path @user = Factory(:user, :password => "Asd123",

How to test attr_accessible fields in RSpec

不想你离开。 提交于 2019-11-30 03:03:06
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} should be accessible" } failure_message_for_should_not { ":#{attribute} should not be accessible" } end But

Rspec testing redirect_to :back

萝らか妹 提交于 2019-11-30 03:02:40
How do you test redirect_to :back in rspec? I get ActionController::RedirectBackError : No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"] . How do I go about setting the HTTP_REFERER in my test? Using RSpec, you can set the referer in a before block. When I tried to set the referer directly in the test, it didn't seem to work no matter where I put it, but the before block does the trick. describe BackController < ApplicationController do before(:each) do request.env[

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

梦想的初衷 提交于 2019-11-29 19:58:32
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 the first test (which was evaluated once in the first test and has the value of 1). Following that, since

Rails: Good Rspec2 example usage? (Also: Cucumber, Pickle, Capybara) [closed]

…衆ロ難τιáo~ 提交于 2019-11-29 19:39:01
I'm looking for a rather recent open source application that uses Rspec 2 as test library. I'd like to see how an experienced developer utilizes the library properly to test the full stack, since I'm constantly in doubt concerning my own knowledge (coming from testunit and partly due to the rather sparse documentation of the latest Rspec release, even though it is constantly improved). If a project would use Cucumber, Pickle and/or Capybara as well together with Rspec 2, you'd have me jumping for joy. Any pointers? Cheers! My 2 cents: Use Steak instead of Cucumber. It RSpec at its core, it is