我在使用Rspec测试时遇到了一种非常奇怪的行为。
我有以下测试来证明问题:
require 'spec_helper'
describe "RESTful API" do
describe "Listing Entities" do
context 'With valid Token' do
before do
entries = create_list(:entry, 10)
admin = create(:admin)
get api_entries_path, {token: admin.api_key.token}
end
it 'Shows a list of entries' do
puts JSON.parse(response.body)
end
it "Should show no entries when the user has no data" do
puts JSON.parse(response.body)
end
end
end
end
非常奇怪的行为是我得到两个不同的输出,而我应该有完全相同的响应。我得到以下内容:
{"entries"=>[{"id"=>1, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>2, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>3, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>4, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>5, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>6, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>7, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>8, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>9, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}, {"id"=>10, "user_id"=>1, "username"=>"admin", "distance"=>50.0, "time_period"=>60, "date_time"=>"2014-05-15 03:36:11 +0300", "created_at"=>"2014-05-15T01:54:40.000Z", "distance_type"=>"Km"}], "total_count"=>10}
{"entries"=>[], "total_count"=>0}
如你所见,我有两种不同的输出。
对于User和ApiKey模型:
# Controller
class Api::V1::EntriesController < Api::V1::BaseController
def index
@entries = @current_user.entries
end
end
class User < ActiveRecord::Base
# relationships
has_many :entries
has_one :api_key, dependent: :destroy
# Generate API Key to be useed with the API
after_create :create_api_key
private
def create_api_key
ApiKey.create!(:user_id => self.id)
end
end
class ApiKey < ActiveRecord::Base
belongs_to :user
before_create :generate_token
private
def generate_token
begin
self.token = SecureRandom.hex.to_s
end while self.class.exists?(token: token)
end
end
请注意我的DatabaseCleaner如下:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
请注意我使用的是ruby(2.0),rails(4.0.4)和rspec-rails(2.14.2)。我正在使用OS X 10.7。
任何帮助将不胜感激。
答案 0 :(得分:1)
经过几个小时的挣扎后,事实证明我必须改变:
DatabaseCleaner.strategy = :transaction
到:
DatabaseCleaner.strategy = : truncation
截断正在将id重置为0(井1),而事务不是,我在工厂使用硬编码ID。
感谢你的帮助Jon Rowe。