[rspec-users] My first tests, backwards. Please help

David Chelimsky dchelimsky at gmail.com
Wed Dec 31 10:18:14 EST 2008


On Wed, Dec 31, 2008 at 3:52 AM, Jesse Crockett <lists at ruby-forum.com> wrote:
> Good new and good news.
>
> First, I'm starting from the floor up.  (The groundwork is still past my
> anxiety threshhold.)  I've saved all the preexisting work to a
> refactor_me file in the rails_root; starting over:

Congrats!

>
> == test code
>
> require File.dirname(__FILE__) + '/../spec_helper'
>
> describe "bidding on an item" do
>  controller_name :items
>
>  before :each do
>    @user = User.first
>    User.stub!(:current_user).with(@user)

Assuming this is a controller spec, you're going to want to stub
controller.current_user, not User.current user:

controller.stub!(:current_user).and_return(@user)

>    puts @user.id, @user.login
>  end
>
>  it "should require a credit" do
>    @user.credits = 0
>    @user.save
>    post 'bid', :bid => {:auction_id => 1, :user_id => @user, :point =>
> 1}
>    assigns[:bid].should_not be_new_record
>  end
>
>  it "should be allowed with a credit" do
>    @user.credits = 1
>    @user.save
>    post 'bid', :bid => {:auction_id => 1, :user_id => @user, :point =>
> 1}
>    assigns[:bid].should be_new_record
>  end
>
> end
>
> == application code
>
>  def bid
>
>    if can_bid?(current_user)
>      @bid = Bid.new(params[:bid])
>      @bid.save
>    end
>
>  end
>
>  def can_bid?(user)
>    user.credits > 0
>  end
>
> == Errors
>
> 1
> quentin
> F1
> quentin
>
> == Was expecting
> 1
> A
> F1
> A

Not sure why you're expecting "A" here. In before(:each) the #puts
statement prints the id of User.first (which is 1) and the login of
that same user (which is "quentin"). The "F" is for a failing example
and there should be another one somewhere in the output (or a "." if
one is passing).

>
> (rather than the bort fixtures)
>
> == auction_spec.rake
>
> namespace :db do
>  desc "Erase and fill the test database for auction spec"
>  task :auction_spec => "test" do
>    require 'populator'
>    require 'faker'
>
>
>    [Item, Auction, User, Charity].each(&:delete_all)

If you use transactional_fixtures, this should not be necessary.
transactional_fixtures is the wrong name - it really means
transactional examples - each example is run in a transaction that is
rolled back. So if you start w/ a truncated db, each example fires up
a transaction, loads up fixtures, runs the example, rolls back the
transaction. Make sense?

>     .
>     .
>     .
>    # Create activated non-admin user
>    user = User.create do |u|
>      u.login = 'A'
>      u.password = u.password_confirmation = 'foobar'
>      u.email = 'a at pandawin.com'
>      u.pref_charity_one_id = 1
>      u.pref_charity_two_id = 2
>      u.pref_charity_three_id = 3
>      u.credits = 10
>    end
>
>    # Activate non-admin user
>    user.register!
>    user.activate!

You can keep these lines in the block - I would, though I wouldn't
populate fixture data in a custom rake task like this.

If you're using fixtures, just make this a fixture in spec/fixtures/users.yml.

But I would recommend using something other than fixtures for sample
data. There are several problems with fixtures, some of which have
been resolved by the rails team over time, but the one that still
remains is that you have to look at another file to understand your
example data.

There are several libraries that help with this problem by letting you
set up default model builders that know how to build a valid model
with reasonable (what reasonable means is up to you - they are user
defined) defaults, but then override those defaults in your examples.

Take a look at these:

http://github.com/flogic/object_daddy/tree/master
http://github.com/thoughtbot/factory_girl/tree/master
http://github.com/notahat/machinist/tree/master

All three are very popular, work well, and offer different approaches
to setting up the default data, but all three let you override that
data in your code examples. Take a look at their READMEs and see if
this makes sense. Feel free to post back if you have questions.

Cheers,
David



>    .
>    .
>    .
>  end
> end
>
> ==
>
> I feel open to constructive criticism, now,  as to the method of using
> test records.  Though I'm not sure what my question is..
>
> Thanks in advance.
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list