简体   繁体   English

控制器操作未在rspec测试中运行

[英]Controller action not running in rspec test

I'm trying to test the index action of a controller that I have. 我正在尝试测试我拥有的控制器的索引操作。 I want it to return instances of something called Moments, which belong to a user. 我希望它返回属于用户的名为Moments的实例。 I am using FactoryGirl and Rspec. 我正在使用FactoryGirl和Rspec。 I verify the user has moments, as when I call @user.moments , it returns them. 我验证用户有时间,就像我调用@user.moments ,它返回它们。 However when I use get :index , it returns a 200 with an empty response.body . 但是,当我使用get :index ,它返回一个带有空response.body的200。

Here is my Rspec: 这是我的Rspec:

require 'spec_helper'

describe Api::V1::MomentsController do

    before do
        @moment = FactoryGirl.create(:moment)
    end
    it 'should return moments' do
        expect(controller).to receive(:index)
        @user = @moment.user
        request.env['Authorization'] = "Token token=#{@user.authentication_token}"
        get :index
        expect(response.status).to eq(200)
        puts response.body
    end
    after do
        @moment = nil
        @user = nil
    end 
end

Here is my Factory: 这是我的工厂:

FactoryGirl.define do
    def time_rand from = 0.0, to = Time.now 
      Time.at(from + rand * (to.to_f - from.to_f))
    end
    sequence :email do |n|
      "email#{n}@ploonky.com"
    end
    sequence :fullname do |n|
      "Waiter Number#{n}"
    end
    sequence :handle do |n|
      "Person#{n}"
    end

    factory :user, :aliases => [:owner] do
      fullname 
      birthdate "1978-12-12"
      email 
      password "password1"
      password_confirmation "password1"
      handle
      tags {
        Array(10).sample.times.map do
          FactoryGirl.create(:tag)
        end
      }
   end
   sequence :datetime do
     "#{time_rand}"
   end
   factory :moment do
     datetime 
     text "can't wait to get through this busy day"
     association :location, :factory => :location 
     user_id { location.user_id }
     is_public true
     is_deleted false
   end

  factory :location do
    label "home"
    street "248 Mckibbin st"
    city "brooklyn"
    state "NY"
    zip "11206"
    latitude 40.7063048
    longitude -73.9396323 
    association :user, :factory => :user
    country "USA"
    is_deleted false
  end

  factory :tag do 
    value "busy as hell"
    is_deleted false 
  end
end

And finally, the controller action: 最后,控制器动作:

class Api::V1::MomentsController < ApplicationController
  before_filter :restrict_access, :except => :public
  respond_to :json
  def index
    updateDate=params['updated_at'] ? DateTime.iso8601(params['updated_at']) : nil
    include_deleted = params['include_deleted'] ? params['include_deleted'].to_i : nil

    friends_ids=Friendship.get_friends(api_user,Friendship::APPROVED)
    friends_ids=friends_ids.map do |friend_ids|
      if friend_ids
        friend_ids[:id]
      end
    end
    user_ids=friends_ids.append(api_user.id) 

    moments=Moment.user_moments(user_ids, include_deleted == 1)

    #filter out moments older than updateDate
    if updateDate
      moments=moments.where('updated_at>?',updateDate)
    end
    moments=buildFullMoments(moments, api_user)
    render :json => moments
  end
end
expect(controller).to receive(:index)

You've just stubbed your controller action. 你刚刚控制了你的控制器动作。 None of the code in your controller action is now running. 控制器操作中的所有代码现在都没有运行。 Remove this line. 删除此行。

What Derek said and also you need to pass in the format of the request: Derek说的是什么,你还需要传递请求的格式:

get :index, format: :json

since your controller will only respond_to :json 因为你的控制器只会respond_to :json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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