Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with rspec test with a polymorphic class
    text
    copied!<p>I'm using MongoMapper instead of ActiveRecord.</p> <p>I have a User model and a Task model. In the Task model, I have 2 attributes as follow:</p> <ul> <li>Owner</li> <li>Author</li> </ul> <p>Both attributes are User references.</p> <p>Here are the relations between the two models:</p> <p>User.rb</p> <pre><code>has_many :tasks, :as =&gt; :owner </code></pre> <p>Taks.rb</p> <pre><code>belongs_to :owner, :class_name =&gt; "User", :polymorphic =&gt; true </code></pre> <p>I've used RSpec for writing test: (@user is declared before)</p> <pre><code>it "should have many tasks" do another_user = Factory.create(:user, :email =&gt; Faker::Internet.email) task_1 = Factory.create(:task, :owner =&gt; another_user, :author =&gt; another_user) task_2 = Factory.create(:task, :owner =&gt; another_user, :author =&gt; @user) task_3 = Factory.create(:task, :owner =&gt; @user, :author =&gt; @user) another_user.tasks.size.should == 2 end </code></pre> <p>And here is the issue:</p> <pre><code>Failure/Error: another_user.tasks.size.should == 2 expected: 2 got: 3 (using ==) </code></pre> <p>However when I do the same in rails console, I get good results...</p> <p>Here are the factories:</p> <pre><code>Factory.define :user do |u| u.first_name 'Test User' # u.username 'Test User' # u.surname 'TheTest' # u.email 'foo@foobar.com' # u.password 'please' # u.confirmed_at Time.now # end Factory.define :task do |u| u.author nil # u.owner nil # u.subjects [] u.timeframe "" u.initially_placed_at nil u.label "Foo Task" # u.description "A small task description" u.done false u.pinned false u.confidentiality "" end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload