On 8/10/07, David Parker <nemo7467 at gmail.com>
wrote:> Still new to Specs...
> How do I create a spec to test a model method? Specifically, here is my
> spec:
> #testing model
> describe User do
> it "should have many user roles" do
> User.reflect_on_association (:user_roles).should_not be_nil
> end
>
> it "should have many roles though user roles" do
> User.reflect_on_association(:roles).should_not be_nil
> end
>
> it "should know what role it has" do
> #User.should
> end
> end
>
> The last spec is incomplete... basically, I will want the code to be
> something like this:
> class User < ActiveRecord::Base
> has_many :user_roles
> has_many :roles, :through => :user_roles
>
> def has_role?(role)
> self.roles.count(:conditions => [''name = ?'',
role] ) > 0
> end
> end
>
> How would I spec the last method?
If you change has_role? to in_role?, you can say this:
role = Role.create(:name => ''foo'')
user.roles.add(role)
user.should be_in_role(''foo'')
> Thanks
> --
> dp
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>