On 8/12/07, David Parker <<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:nemo7467@gmail.com">nemo7467@gmail.com</a>> wrote:<br>> Hello all, I'm currently having an issue with some specs, specifically scope
<br>> issues (probably more of a scope issue than a spec issue at this point)...<br>> Here's the error:<br>> ActiveRecord::AssociationTypeM<div style="direction: ltr;">ismatch in 'A new User should<br>> know what role it has'
<br>> Role expected, got NilClass<br>> D:/ruby/projects/restful_authentication_test/config/../app/models/role.rb:5:in<br>> `add'<br>> ./spec/models/user_spec.rb:62:<br>> script/spec:4:<br>><br>> Here's the
user.rb<br>> <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://pastie.caboo.se/87029" target="_blank">http://pastie.caboo.se/87029</a><br>><br>> role.rb<br>> <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://pastie.caboo.se/87030" target="_blank">
http://pastie.caboo.se/87030</a><br>><br>> user_spec.rb<br>> <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://pastie.caboo.se/87032" target="_blank">http://pastie.caboo.se/87032</a><br>>
<br>> The code is based off of RESTful authentication +<br>> Ben Curtis' user roles in Rails Application:<br>> <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.bencurtis.com/archives/2007/06/user-roles-in-rails-applications/" target="_blank">
http://www.bencurtis.com/archives/2007/06/user-roles-in-rails-applications/</a><br>><br>users.roles is an AssociationProxy, not the Role class, where you have<br>defined the method add(user, role). You should be able to eliminate
<br>that method in the Role class and just pass the role on line 62 of<br>your spec:<br><br>users.roles.add(role)<br>____________<br>I went ahead an eliminated the add method in the role class, and now, with the same spec as above these are the results I get, depending on what I have for line 62...
<br><br>Users.roles.add(role):<br>NameError in 'A new User should know what role it has'<br>uninitialized constant Users<br><br>User.roles.add(role):<br>NoMethodError in 'A new User should know what role it has'
<br>undefined method `roles' for User:Class<br><br>user.roles.add(role):<br>NoMethodError in 'A new User should know what role it has'<br>undefined method `add' for Role:Class<br><br>users.roles.add(role):
<br>
NameError in 'A new User should know what role it has'<br>
undefined local variable or method `users' <br><br>If I go ahead and do something like:<br>users = User.find(:all)<br>before doing users.roles.add(role), then I get:<br>NoMethodError in 'A new User should know what role it has'
<br>undefined method `roles' <br><br>are my associations somehow not registering?<br><br>><br>> Thanks for your help!<br>> --<br>> dp<br>> _______________________________________________<br>> rspec-users mailing list
<br>> <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>> <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br>><br></div><br clear="all"><br>-- <br><br>dp