Note that there are some explanatory texts on larger screens.

plurals
  1. PORSpec assigns not working as expected
    primarykey
    data
    text
    <p>I have the following spec for the controller in simple ActiveRecord search feature:</p> <p>Spec:</p> <pre><code>it "returns the records that match the given due date" do create(:task, due_date: '2013-01-01') create(:task, due_date: '2014-01-01') get :search, 'filter' =&gt; { due_date: '2013-01-01' } expect(assigns(:tasks)).to \ eq Task.search('filter' =&gt; { due_date: '2013-01-01' }) end </code></pre> <p>The model and controller are simple:</p> <p>Model:</p> <pre><code> def self.search(params) result = self.all #I know this is a bad idea, but starting simple. params.each do |field, criteria| if field.match(/due_date|completed_date/) &amp;&amp; criteria != nil result = result.where("DATE(#{field}) = ?", criteria) end end result end </code></pre> <p>Controller action:</p> <pre><code> def search @tasks = Task.search(params['filter']) #output from when the spec runs below #puts params -&gt; {"filter"=&gt;{"due_date"=&gt;"2013-01-01"}, \ # "controller"=&gt;"tasks", \ # "action"=&gt;"search"} #puts params['filter] -&gt; {"due_date"=&gt;"2013-01-01"} #puts @tasks.inspect -&gt; just the one record render 'index' end </code></pre> <p>The spec fails, but it appears that it fails because the controller is returning both objects, while Task.search(...) is returning only the object with the specified value for <code>due_date</code>, as expected.</p> <p>Here is the error message (edited for length):</p> <pre><code> 2) TasksController GET #search returns the records that match the given due date Failure/Error: expect(assigns(:tasks)).to eq Task.search('filter' =&gt; { due_date: '2013-01-01' }) expected: #&lt;ActiveRecord::Relation [#&lt;Task id: 1, due_date: "2013-01-01 00:00:00", completed_date: "2013-12-22 03:57:37"&gt;, #&lt;Task id: 2, due_date: "2014-01-01 00:00:00", completed_date: "2013-12-22 03:57:37"&gt;]&gt; got: #&lt;ActiveRecord::Relation [#&lt;Task id: 1, due_date: "2013-01-01 00:00:00", completed_date: "2013-12-22 03:57:37"&gt;]&gt; </code></pre> <p>You would assume that since the model apparently works (as evidenced by this result and a separate model spec that passes) that there is something wrong with the controller, but the controller is dead simple. I also have a feature spec incorporating the same controller that submits a form, triggers the <code>search</code> action and looks at the output, and the output only includes the one, correct record.</p> <p>Am I missing something about how <code>assigns</code> works, making a dumb mistake or other?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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