简体   繁体   中英

RSpec assigns not working as expected

I have the following spec for the controller in simple ActiveRecord search feature:

Spec:

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' => { due_date: '2013-01-01' }
  expect(assigns(:tasks)).to \
    eq Task.search('filter' => { due_date: '2013-01-01' })
end

The model and controller are simple:

Model:

  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/) && criteria != nil
        result = result.where("DATE(#{field}) = ?", criteria)
      end
    end
    result
  end

Controller action:

  def search
    @tasks = Task.search(params['filter'])

    #output from when the spec runs below
    #puts params           -> {"filter"=>{"due_date"=>"2013-01-01"}, \
    #                                     "controller"=>"tasks", \
    #                                     "action"=>"search"}

    #puts params['filter]  -> {"due_date"=>"2013-01-01"}
    #puts @tasks.inspect   -> just the one record

    render 'index'
  end

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 due_date , as expected.

Here is the error message (edited for length):

  2) TasksController GET #search returns the records that 
     match the given due date

     Failure/Error: expect(assigns(:tasks)).to 
       eq Task.search('filter' => { due_date: '2013-01-01' })  

     expected: #<ActiveRecord::Relation 
       [#<Task id: 1, 
       due_date: "2013-01-01 00:00:00",
       completed_date: "2013-12-22 03:57:37">,
       #<Task id: 2, due_date: "2014-01-01 00:00:00",
       completed_date: "2013-12-22 03:57:37">]>

     got: #<ActiveRecord::Relation
       [#<Task id: 1,
       due_date: "2013-01-01 00:00:00",
       completed_date: "2013-12-22 03:57:37">]>

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 search action and looks at the output, and the output only includes the one, correct record.

Am I missing something about how assigns works, making a dumb mistake or other?

It was option B, dumb mistake.

The model method takes the value of the filter element of the params hash as an argument, not the fake params hash I need to send to GET #search in the line above the expectation. Or more clearly maybe, replace:

expect(assigns(:tasks)).to eq Task.search('filter' => { due_date: '2013-01-01' })

with

expect(assigns(:tasks)).to eq Task.search(due_date: '2013-01-01')'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM