Missing model attribute in cucumber test

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:inmethod_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:inAnd the user named
“newuser” is an administrator’

Have I misspelled something somewhere and just cannot see it? what is
causing this?

James B. 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:inmethod_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:inAnd 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.

James B. 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?

On Wed, Jan 14, 2009 at 7:46 PM, James B. [email protected]
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

On Wed, Jan 14, 2009 at 7:56 PM, James B. [email protected]
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

James B.
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
[email protected]
http://nando.lacoctelera.com

The Cocktail
C/ Salamanca 17
28020 Madrid
+34 91 567 06 05

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

On 14 Jan 2009, at 18:46, James B. 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 W.
http://blog.mattwynne.net