[rspec-users] Newbie : How to spec params hash amendment
I am new to rspec and am finding it all a bit daunting at the moment. I'd like to be able to adopt a bdd approach to my development but I'm still at the stage of trying to get my head around rspec concepts so I'm committing the cardinal sin of coding first and then writing tests. I am using the following: Ruby v1.8.7 Rails v2.3.8 Authlogic v2.1.5 Cancan v1.2.0 rspec v1.3.0 rspec-rails v1.3.2 webrat v0.7.1 cucumber-rails v0.3.2 capybara v0.3.9 factory_girl v1.2.4 pickle v0.3.0 no_peeping_toms v1.1.0 email_spec v0.6.2 In spec_helper.rb, I have def current_user(stubs = {}) @current_user ||= mock_model(User, stubs) end def user_session(stubs = {}, user_stubs = {}) @current_user_session ||= mock_model(UserSession, {:user => current_user(user_stubs)}.merge(stubs)) end def login(session_stubs = {}, user_stubs = {}) UserSession.stub!(:find).and_return(user_session(session_stubs, user_stubs)) end def logout @user_session = nil end def login_as_admin login({}, {:role => 'admin'}) @current_user.stub(:role?).with(:admin).and_return(true) end In ability.rb (for cancan) def initialize(user) user ||= User.new # Guest user if user.role? :admin can [:index, :show, :new, :edit, :create, :update, :destroy], [User, CommonContents, Logo] else can [:show, :edit, :update], User cannot [:index, :show, :new, :edit, :create, :update, :destroy], [CommonContents, Logo] end end In UserController def create # New users can only be created by admins authorize! :create, User # When the user is first defined by admin, the password is not set # Create a random one to satisfy validation requirements, which the user changes when they first visit params[:user][:password] = random_password() params[:user][:password_confirmation] = params[:user][:password] @user = User.new(params[:user]) if @user.save redirect_to(@user, :notice => 'User was successfully created.') else @roles = User.const_get("ADMIN_CAN_CREATE") render :action => "new" end end I have a private method in ApplicationController as follows: def random_password(length = 10) alphanumerics = [('0'..'9'),('A'..'Z'),('a'..'z')].map {|range| range.to_a}.flatten (0...length).map { alphanumerics[Kernel.rand(alphanumerics.size)] }.join end My spec is currently describe "when authorised as admin" do describe "POST 'create'" do before(:each) do login_as_admin end after(:each) do logout end describe "with valid params" do it "should set password and password_confirmation to the same 10 digit value" do # ? end end end end I have no idea how to approach 'rspec'ing the creation of the params[:user][:password] and :password_confirmation or the random_password method and would very much appreciate it if somebody could spare the time to suggest what I should be doing. ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] Problem testing Authlogic UserSession
I've posted this to the Authlogic group but things seem a little quiet, so perhaps someone here can help: 'm testing Authlogic 2.1.6, running under Rails 3.0.1., using rspec 2.1.0 I have the following: user.rb class User < ActiveRecord::Base acts_as_authentic do |config| config.login_field :email config.ignore_blank_passwords false config.disable_perishable_token_maintenance true end end user_session.rb class UserSession < Authlogic::Session::Base end user_sessions_controller_spec.rb require 'spec_helper' def user_session_create @user = Factory.create(:user) @user_session = UserSession.create(:email => @user.email, :password => @user.password) end describe UserSessionsController do before(:each) do :activate_authlogic end describe "create a user session" do it "creates a user session using the user credentials" do user_session_create @user_session.email.should == @user.email end end describe "find a user session" do it "locates the user session" do user_session_create us = UserSession.find us.email.should == @user.email end end end I have the correct lines in spec_helper.rb ( require "authlogic/test_case" and include Authlogic::TestCase) The create a user session test is ok but the find a user session test is failing with Failures: 1) UserSessionsController find a user session locates the user session Failure/Error: us.email.should == @user.email expected: "ron2.weasl...@hogworts.com", got: nil (using ==) When I try this using console, I get the following ruby-1.8.7-p302 > require "authlogic/test_case" => true ruby-1.8.7-p302 > include Authlogic::TestCase => Object ruby-1.8.7-p302 >ron = User.first ruby-1.8.7-p302 > activate_authlogic => #"text/html"}> ruby-1.8.7-p302 > us = UserSession.new(:email => ron.email, :password => ron.password) => #"", :email=>"ron1.weasl...@hogworts.com"}> ruby-1.8.7-p302 > us.save NoMethodError: undefined method `remote_ip' for nil:NilClass from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ activesupport-3.0.1/lib/active_support/whiny_nil.rb:48:in `method_missing' from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ authlogic-2.1.6/lib/authlogic/session/magic_columns.rb:61:in `update_info' from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ activesupport-3.0.1/lib/active_support/callbacks.rb:414:in `_run_before_save_callbacks' from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ activesupport-3.0.1/lib/active_support/callbacks.rb:93:in `send' from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ activesupport-3.0.1/lib/active_support/callbacks.rb:93:in `run_callbacks' from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ authlogic-2.1.6/lib/authlogic/session/callbacks.rb:83:in `before_save' from /Users/martin/.rvm/gems/ruby-1.8.7-p...@pta3/gems/ authlogic-2.1.6/lib/authlogic/session/existence.rb:68:in `save' from (irb):9 I'm at a bit of a loss to explain this - any help very welcome! ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] Speccing a model class method in Rails produces nil
Ruby 1.9.2, Rails 3.0.3, Rspec-rails 2.2.0 I have: Factory.define :season_date do |f| f.season_date Date.new(2011,9,24) f.date_type "season_start" end RSpec.configure do |config| config.mock_with :rspec end Factory.define :season_date do |f| f.season_date Date.new(2011,9,24) f.date_type "season_start" end Factory.define :season_date do |f| f.season_date Date.new(2011,9,24) f.date_type "season_start" end require 'spec_helper' describe SeasonDate do before(:each) do @start_date_record = Factory.create(:season_date) @no_play_date_record = Factory.create(:season_date, season_date: Date.today, date_type: "no_play") end it "responds to the find_start_record method call" do SeasonDate.should respond_to(:find_start_record) end it "returns the record with the season start date" do SeasonDate.find_start_record.should == @start_date_record end end and I get rspec -f d -b spec/models/season_date_spec.rb SeasonDate responds to the find_start_record method call returns the record with the season start date (FAILED - 1) Failures: 1) SeasonDate returns the record with the season start date Failure/Error: SeasonDate.find_start_record.should == @start_date_record expected: #, got: nil (using ==) I'm doing something dumb, but I don't know what. Any assistance much appreciated! ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Speccing a model class method in Rails produces nil
I can't believe I did that... No, there is only one Factory; copy and paste error. The SeasonDate class is: class SeasonDate < ActiveRecord::Base def self.find_start_record where(["date_type = ?", "start_date"]).first end end Agreed regarding the redundant test - I just put that in when things went wrong. Thanks for responding On Dec 4, 2:25 am, David Chelimsky wrote: > On Dec 3, 2010, at 11:07 AM, Martin Hawkins wrote: > > > > > > > Ruby 1.9.2, Rails 3.0.3, Rspec-rails 2.2.0 > > > I have: > > > Factory.define :season_date do |f| > > f.season_date Date.new(2011,9,24) > > f.date_type "season_start" > > end > > > RSpec.configure do |config| > > config.mock_with :rspec > > end > > > Factory.define :season_date do |f| > > f.season_date Date.new(2011,9,24) > > f.date_type "season_start" > > end > > > Factory.define :season_date do |f| > > f.season_date Date.new(2011,9,24) > > f.date_type "season_start" > > end > > Do you actually have 3 identical factories or was that just a copy/paste > error? > > > require 'spec_helper' > > describe SeasonDate do > > before(:each) do > > �...@start_date_record = Factory.create(:season_date) > > �...@no_play_date_record = Factory.create(:season_date, season_date: > > Date.today, date_type: "no_play") > > end > > it "responds to the find_start_record method call" do > > SeasonDate.should respond_to(:find_start_record) > > end > > it "returns the record with the season start date" do > > SeasonDate.find_start_record.should == @start_date_record > > The fact that this example ^^ sends SeasonDate the find_start_record message > makes the previous example unnecessary. > > > > > > > end > > end > > > and I get > > > rspec -f d -b spec/models/season_date_spec.rb > > SeasonDate > > responds to the find_start_record method call > > returns the record with the season start date (FAILED - 1) > > > Failures: > > > 1) SeasonDate returns the record with the season start date > > Failure/Error: SeasonDate.find_start_record.should == > > @start_date_record > > expected: # > date_type: "season_start", created_at: "2010-12-03 17:03:58", > > updated_at: "2010-12-03 17:03:58">, > > got: nil (using ==) > > > I'm doing something dumb, but I don't know what. Any assistance much > > appreciated! > > Can't really tell from what you've posted. Please post the implementation of > SeasonDate.season_date. > ___ > rspec-users mailing list > rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Speccing a model class method in Rails produces nil
Apologies if this is a repeat post but google seemed to ignore my last effort. There is only one Factory - copy, paste and editing error. I can't believe that I didn't include the code being tested... class SeasonDate < ActiveRecord::Base def self.find_start_record where(["date_type = ?", "start_date"]).first end end Agreed re redundant test - I put that in when the 'real' test failed. On Dec 4, 2:25 am, David Chelimsky wrote: > On Dec 3, 2010, at 11:07 AM, Martin Hawkins wrote: > > > > > > > Ruby 1.9.2, Rails 3.0.3, Rspec-rails 2.2.0 > > > I have: > > > Factory.define :season_date do |f| > > f.season_date Date.new(2011,9,24) > > f.date_type "season_start" > > end > > > RSpec.configure do |config| > > config.mock_with :rspec > > end > > > Factory.define :season_date do |f| > > f.season_date Date.new(2011,9,24) > > f.date_type "season_start" > > end > > > Factory.define :season_date do |f| > > f.season_date Date.new(2011,9,24) > > f.date_type "season_start" > > end > > Do you actually have 3 identical factories or was that just a copy/paste > error? > > > require 'spec_helper' > > describe SeasonDate do > > before(:each) do > > �...@start_date_record = Factory.create(:season_date) > > �...@no_play_date_record = Factory.create(:season_date, season_date: > > Date.today, date_type: "no_play") > > end > > it "responds to the find_start_record method call" do > > SeasonDate.should respond_to(:find_start_record) > > end > > it "returns the record with the season start date" do > > SeasonDate.find_start_record.should == @start_date_record > > The fact that this example ^^ sends SeasonDate the find_start_record message > makes the previous example unnecessary. > > > > > > > end > > end > > > and I get > > > rspec -f d -b spec/models/season_date_spec.rb > > SeasonDate > > responds to the find_start_record method call > > returns the record with the season start date (FAILED - 1) > > > Failures: > > > 1) SeasonDate returns the record with the season start date > > Failure/Error: SeasonDate.find_start_record.should == > > @start_date_record > > expected: # > date_type: "season_start", created_at: "2010-12-03 17:03:58", > > updated_at: "2010-12-03 17:03:58">, > > got: nil (using ==) > > > I'm doing something dumb, but I don't know what. Any assistance much > > appreciated! > > Can't really tell from what you've posted. Please post the implementation of > SeasonDate.season_date. > ___ > rspec-users mailing list > rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Speccing a model class method in Rails produces nil
I can't believe I did that either... Doh! On Dec 4, 4:04 pm, David Chelimsky wrote: > On Sat, Dec 4, 2010 at 8:34 AM, Martin Hawkins > wrote: > > I can't believe I did that... > > No, there is only one Factory; copy and paste error. The SeasonDate > > class is: > > class SeasonDate < ActiveRecord::Base > > def self.find_start_record > > where(["date_type = ?", "start_date"]).first > > I think the problem is "start_date" vs "season_start". The factory says: > > Factory.define :season_date do |f| > f.season_date Date.new(2011,9,24) > f.date_type "season_start" > end > > date_type == "start_date" # in the implementation > date_type == "season_start" # in the factory > > HTH, > David > > > > > > > end > > end > > Agreed regarding the redundant test - I just put that in when things > > went wrong. > > Thanks for responding > > > On Dec 4, 2:25 am, David Chelimsky wrote: > >> On Dec 3, 2010, at 11:07 AM, Martin Hawkins wrote: > > >> > Ruby 1.9.2, Rails 3.0.3, Rspec-rails 2.2.0 > > >> > I have: > > >> > Factory.define :season_date do |f| > >> > f.season_date Date.new(2011,9,24) > >> > f.date_type "season_start" > >> > end > > >> > RSpec.configure do |config| > >> > config.mock_with :rspec > >> > end > > >> > Factory.define :season_date do |f| > >> > f.season_date Date.new(2011,9,24) > >> > f.date_type "season_start" > >> > end > > >> > Factory.define :season_date do |f| > >> > f.season_date Date.new(2011,9,24) > >> > f.date_type "season_start" > >> > end > > >> Do you actually have 3 identical factories or was that just a copy/paste > >> error? > > >> > require 'spec_helper' > >> > describe SeasonDate do > >> > before(:each) do > >> > �...@start_date_record = Factory.create(:season_date) > >> > �...@no_play_date_record = Factory.create(:season_date, season_date: > >> > Date.today, date_type: "no_play") > >> > end > >> > it "responds to the find_start_record method call" do > >> > SeasonDate.should respond_to(:find_start_record) > >> > end > >> > it "returns the record with the season start date" do > >> > SeasonDate.find_start_record.should == @start_date_record > > >> The fact that this example ^^ sends SeasonDate the find_start_record > >> message makes the previous example unnecessary. > > >> > end > >> > end > > >> > and I get > > >> > rspec -f d -b spec/models/season_date_spec.rb > >> > SeasonDate > >> > responds to the find_start_record method call > >> > returns the record with the season start date (FAILED - 1) > > >> > Failures: > > >> > 1) SeasonDate returns the record with the season start date > >> > Failure/Error: SeasonDate.find_start_record.should == > >> > @start_date_record > >> > expected: # >> > date_type: "season_start", created_at: "2010-12-03 17:03:58", > >> > updated_at: "2010-12-03 17:03:58">, > >> > got: nil (using ==) > > >> > I'm doing something dumb, but I don't know what. Any assistance much > >> > appreciated! > > >> Can't really tell from what you've posted. Please post the implementation > >> of SeasonDate.season_date. > >> ___ > >> rspec-users mailing list > >> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > ___ > > rspec-users mailing list > > rspec-us...@rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > ___ > rspec-users mailing list > rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Problem with 1.9.2
I had this problem too. I am in a project that has 1.9.2 and a similar list of gems. I'm afraid I chickened out of using autotest and use watchr instead. On Dec 6, 9:49 pm, Nicholas Wieland wrote: > Hi guys, I'm not entirely sure this is a problem with RSpec or cucumber, for > sure it's happening after the last gem update under 1.9.2 (rvm). > > Backtrace:https://gist.github.com/730995 > Gemfile:https://gist.github.com/730999 > > I've tried to search the archives but even if the "circular require > considered harmful" seems already well known, it doesn't seem to match my > case. > > Does someone have suggestions about how to solve this ? In ree everything > works as expected. > > ngw > > -- > Nicholas Wieland (ngw) > Zooppa CTO > 911 Western Avenue, Suite 420 > Seattle, WA 98104 US > > ___ > rspec-users mailing list > rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] major release required?
I agree with you that to create a major release for this too grand. Suitable fanfare and documentation is fine. On 18 January 2011 14:15, David Chelimsky wrote: > Hi all, > > Since the release of rspec-2.0, I've been following Rubygems' rational > versioning [1] as closely as possible. Patch releases (2.4.x) have only had > bug fixes, and minor releases (2.x.0) have had new features, but no > (intentionally) backward incompatible changes, which should require a major > (3.0) release. > > The autotest extension in rspec-2.0 prefixes the command it generates with > 'bundle exec' if it sees a 'Gemfile' in the project root directory. It turns > out that this is not universally helpful, so there was a request to have an > opt-out. > > It also turns out that autotest has a bundler plugin that prefixes the > command with 'bundle exec'. To use an autotest plugin, you just require it > in a .autotest file in the project root. In this case: > > require 'autotest/bundler' > > I think the right thing to do is to rely on the autotest plugin, but I also > think that this would require a 3.0 release, which feels a bit grand for > this situation. My question to you is: do you think this warrants a major > (3.0) release, or would it be an acceptable exception to the rule (assuming > proper fanfare and documentation)? > > [1] http://docs.rubygems.org/read/chapter/7 > > Cheers, > David > > > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users