Forum: RSpec rspec and shoulda have_indices

78e7109f5fd74ae07678541288f0a417?d=identicon&s=25 slavix tube (slavix)
on 2011-09-02 11:26
(Received via mailing list)
Hello,
I am developing a rails 3.1 app. My Gemfile has shoulda gems
Gemfile
..
gem "shoulda"
gem "shoulda-matchers"

In a model spec I have
spec file
  it { should have_indices([:user, :currency]) }

And I get this error...

NoMethodError:
       undefined method `has_indices?' for #<Balance:0xf3b860c>
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2011-09-02 16:14
(Received via mailing list)
On Sep 2, 2011, at 4:12 AM, slavix wrote:

> Hello,
> I am developing a rails 3.1 app. My Gemfile has shoulda gems
> Gemfile
> ..
> gem "shoulda"
> gem "shoulda-matchers"

You only need shoulda-matchers if you're using rspec.

> In a model spec I have
> spec file
>  it { should have_indices([:user, :currency]) }
>
> And I get this error...
>
> NoMethodError:
>       undefined method `has_indices?' for #<Balance:0xf3b860c>

When there is no have_xxx method defined, rspec-expectations implements
method_missing such that it assumes you want to send the object the
has_xxx? predicate. What your experiencing suggests that there is no
have_indices method. Looking at
http://rdoc.info/github/thoughtbot/shoulda-matcher...,
I see a singular have_db_index matcher, but I don't see a pluralized
one, so perhaps this method doesn't exist. If it does, then something is
wrong with your configuration, as it is not being made available.

HTH,
David
78e7109f5fd74ae07678541288f0a417?d=identicon&s=25 slavix tube (slavix)
on 2011-09-03 01:15
(Received via mailing list)
Thanks,
I updated spec with correct statements

  it { should have_db_index([:user, :currency, :tradable]) }
----------
and my db (dev and test) has the indexes

Indexes:
Action  Keyname  Type  Unique  Packed  Field  Cardinality  Collation
Null
Comment
Edit   Drop   PRIMARY  BTREE  Yes  No  id  0  A
Edit   Drop   index_balances_on_currency_id  BTREE  No  No  currency_id
0  A
YES
Edit   Drop   index_balances_on_tradable_id  BTREE  No  No  tradable_id
0  A
YES
Edit   Drop   index_balances_on_user_id  BTREE  No  No  user_id  0

but I get an error running rspec
  2) Balance shoulda validations
     Failure/Error: it { should
have_db_index([:user, :currency, :tradable]) }
       Expected Balance to have a  index on columns user and currency
and tradable ()
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2011-09-03 02:05
(Received via mailing list)
I moved your post to the bottom. Please read
http://idallen.com/topposting.html

On Sep 2, 2011, at 5:58 PM, slavix wrote:


>> You only need shoulda-matchers if you're using rspec.
>> When there is no have_xxx method defined, rspec-expectations implements
method_missing such that it assumes you want to send the object the has_xxx?
predicate. What your experiencing suggests that there is no have_indices method.
Looking
athttp://rdoc.info/github/thoughtbot/shoulda-matcher......, I 
see
a singular have_db_index matcher, but I don't see a pluralized one, so perhaps
this method doesn't exist. If it does, then something is wrong with your
configuration, as it is not being made available.
> Edit   Drop   PRIMARY  BTREE  Yes  No  id  0  A
>       Expected Balance to have a  index on columns user and currency
> and tradable ()

The indexes are on xxx_id. Try { should have_db_index([:user_id,
:currency_id, :tradable_id]) }
78e7109f5fd74ae07678541288f0a417?d=identicon&s=25 slavix tube (slavix)
on 2011-09-03 03:06
(Received via mailing list)
now getting
Failure/Error: it { should
have_db_index([:user, :currency, :tradable]) }
       Expected Balance to have a  index on columns user and currency
and tradable ()

but the indexes are in the db.. (dev and test)
Indexes:
Action          Keyname                                         Type
Unique  Packed  Field  Cardinality  Collation  Null  Comment
Edit   Drop   PRIMARY                                  BTREE  Yes  No
id  0
A
Edit   Drop   index_balances_on_currency_id  BTREE  No  No  currency_id
0  A
YES
Edit   Drop   index_balances_on_tradable_id  BTREE  No  No  tradable_id
0  A
YES
Edit   Drop   index_balances_on_user_id           BTREE  No  No  user_id
0  A
YES

my migration has
def self.up
    create_table :balances do |t|
      t.belongs_to :user, :nil => false
      t.belongs_to :currency, :nil => false
      t.belongs_to :tradable, :nil => false
      t.decimal :amount, :precision => 16, :scale => 8, :default =>
0.0

      t.timestamps
    end
    add_index :balances, :user_id
    add_index :balances, :currency_id
    add_index :balances, :tradable_id
  end
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (mguterl)
on 2011-09-03 15:14
(Received via mailing list)
My guess is that it is checking for an compound index on all of those
fields.  Try specifying them individually and not in an array.
78e7109f5fd74ae07678541288f0a417?d=identicon&s=25 slavix tube (slavix)
on 2011-09-04 03:34
(Received via mailing list)
Yes. Thank you. I changed the code to list indices one at a time and
it worked.

    it { should have_db_index(:currency_id) }
    it { should have_db_index(:user_id) }
This topic is locked and can not be replied to.