我有Rails 4.2.6和rspec 3.3.0,设计版本3.5.8我无法测试我的控制器规格总是收到错误说 ("预计响应有一个成功状态代码(2xx),但它是401") 稍后,当我尝试不同的规格时,我总是收到失败消息 ("您的帐户尚未启用!") ,任何想法?,任何帮助非常感谢..
我的规格:
require 'rails_helper'
describe MyController, :type => :controller
let(:user) { create(:user) }
let(:campaign) { create(:campaign, user: user) }
let!(:placement) { create(:placement, user: user, campaign: campaign, end_date: Date.today) }
before(:each) do
sign_in user
end
context "when we have no data" do
before do
get :graph_data, format: :json
@json = JSON.parse(response.body)
end
it "should be a success" do
expect(response).to have_http_status(:success)
end
end
end
我有以下spec_helper:
config.include Devise::TestHelpers, :type => :controller
config.before(:each) do
DatabaseCleaner.start
end
工厂定义:
FactoryGirl.define do
factory :unconfirmed_user, class: User do
sequence(:email) { |n| "john#{n}@email.com" }
sequence(:name) { |n| "John Nice #{n}" }
password 'password'
password_confirmation 'password'
factory :user do
confirmed_at Time.now.utc
factory :admin do
organisation { create(:organisation, :some_organisation) }
end
end
end
控制器:
MyController < ApplicationController
def graph_data
items = current_user.items //outputs [1,2,3,45,5]
render json: items, status: :ok
end
end
路线:
GET /mycontroller/graph_data(.:format)
答案 0 :(得分:0)
我发现问题是什么,用户模型有一个方法&#39; active_for_authentication?&#39;
def active_for_authentication?
super && (group.include?('company-group) || is_admin? )
end
方法&#39; active_for_authentication?&#39;是一种设计公共方法 当它在你的模型中定义为(用户)时,你改变它的行为,在我的情况下,它被不同的行为覆盖, 检查属于该公司组的所属用户&#39;或不 .. 这就是为什么我有失败的消息(&#34;您的帐户尚未启用!&#34;)