Forum: RSpec Missing model attribute in cucumber test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-14 19:46
Rails 2.2.2
Cucumber 0.1.15
SQLite3

I have this migration for the User model:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
...
      t.string    :sms_address
      t.boolean   :user_administrator,    :default => false,
                                          :null => false
      t.string    :username,              :null => false
...

I see this in the DB schema:

CREATE TABLE "users" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
...
  "sms_address" varchar(255),
  "user_administrator" boolean DEFAULT 'f' NOT NULL,
  "username" varchar(255) NOT NULL,
...

And in the console I see this:

>> myuser=User.new
=> #<User id: nil, crypted_password: nil, current_login_at: nil,
current_login_ip: nil, email: nil, last_login_at: nil, last_login_ip:
nil, last_request_at: nil, login_count: nil, name_last: nil, name_first:
nil, name_middle: nil, openid_identifier: nil, password_salt: nil,
perishable_token: nil, persistence_token: nil, single_access_token: nil,
sms_address: nil, user_administrator: false, username: nil, created_at:
nil, updated_at: nil>
?>
>> y myuser.attributes.sort
---
...
  -
- - sms_address
  -
- - updated_at
  -
- - user_administrator
  - false
- - username
  -
=> nil

So, as far as I can see user_administrator is an attribute of users.

However, when I have this step defintion:

When /user named "(.*)" is an administrator/ do |name|
  my_user = User.find_by_username!(name)
  my_user.user_administrator
end

Then I see this error:

    And the user named "newuser" is an administrator     #
features/app/models/users/step_definitions/user_steps.rb:24
      undefined method `user_administrator' for #<User:0x2ac26fa83e80>
(NoMethodError)
      /usr/lib64/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:260:in
`method_missing'
      ./features/app/models/users/step_definitions/user_steps.rb:26:in
`And /user named "(.*)" is an administrator/'
      features/app/models/users/user.feature:29:in `And the user named
"newuser" is an administrator'


Have I misspelled something somewhere and just cannot see it?  what is
causing this?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-14 19:56
James Byrne wrote:

> Then I see this error:
>
>     And the user named "newuser" is an administrator     #
> features/app/models/users/step_definitions/user_steps.rb:24
>       undefined method `user_administrator' for #<User:0x2ac26fa83e80>
> (NoMethodError)
> 
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:260:in
> `method_missing'
>       ./features/app/models/users/step_definitions/user_steps.rb:26:in
> `And /user named "(.*)" is an administrator/'
>       features/app/models/users/user.feature:29:in `And the user named
> "newuser" is an administrator'
>
>
> Have I misspelled something somewhere and just cannot see it?  what is
> causing this?

I have changed the type to string and get the same error.  I have
changed the attribute name to useradministrator and still get the same
error.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-14 20:18
James Byrne wrote:

I have gotten past this somehow.  The only thing that I think may have
influenced this is manually preparing the test database.  However, now I
have a different problem.

Here is the same step definition:

When /user named "(.*)" is an administrator/ do |name|
  my_user = User.find_by_username!(name)
  puts my_user.administrator.to_s
  my_user.administrator
end

This produces this output in a cucumber run:

false
    And the user named "newuser" is an administrator     #
features/app/models/users/step_definitions/user_steps.rb:24

Notice the value reported from the puts statement.  Yet, this feature
step passes.   What am I doing wrong?
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-01-14 20:48
(Received via mailing list)
On Wed, Jan 14, 2009 at 7:46 PM, James Byrne <lists@ruby-forum.com>
wrote:

>      t.string    :sms_address
>  "sms_address" varchar(255),
> nil, name_middle: nil, openid_identifier: nil, password_salt: nil,
> - - updated_at
>
> (NoMethodError)
> causing this?
Did you migrate your test db? rake features does that for you, but plain
cucumber doesn't

Aslak
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-01-14 21:07
(Received via mailing list)
On Wed, Jan 14, 2009 at 7:56 PM, James Byrne <lists@ruby-forum.com>
wrote:

> > `method_missing'
> changed the attribute name to useradministrator and still get the same
> error.


Some things you can try

* Open a raw database session against the test db and look at tables and
columns
* script/runner "p User.column_names" -e test
* script/runner "p User.column_names" -e development
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-01-14 21:25
(Received via mailing list)
On 14 Jan 2009, at 18:46, James Byrne wrote:

> `And /user named "(.*)" is an administrator/'
>      features/app/models/users/user.feature:29:in `And the user named
> "newuser" is an administrator'


Try rake db:test:prepare maybe?

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com
75cf3e99d398f414c227a63bcdb46b51?d=identicon&s=25 Fernando García Samblas (Guest)
on 2009-01-14 22:16
(Received via mailing list)
James Byrne
escribió:>   puts my_user.administrator.to_s
>   my_user.administrator
> end
>

I think the last statement of the step definition "should be":

  my_user.administrator.should be_true

for an rspec based implementation, or

  fail unless my_user.administrator

 for a pure cucumber one.

> This produces this output in a cucumber run:
>
> false
>     And the user named "newuser" is an administrator     #
> features/app/models/users/step_definitions/user_steps.rb:24
>
> Notice the value reported from the puts statement.  Yet, this feature
> step passes.   What am I doing wrong?
>

--
Fernando García Samblas
fernando.garcia@the-cocktail.com
http://nando.lacoctelera.com

The Cocktail
C/ Salamanca 17
28020 Madrid
+34 91 567 06 05
0be0e4aa42aacd9a8a95c792de273ca7?d=identicon&s=25 Aslak Hellesøy (aslakhellesoy)
on 2009-01-14 22:30
(Received via mailing list)
>  my_user = User.find_by_username!(name)
> Notice the value reported from the puts statement.  Yet, this feature
> step passes.   What am I doing wrong?

Where did you expect it to fail? This will fail:

my_user.should be_administrator

False return values never fails unless you use #should like above.

Aslak
This topic is locked and can not be replied to.