即时RSpec测试驱动开发方法强参数错误

时间:2014-08-05 11:40:49

标签: ruby-on-rails-4 rspec

我一直在努力完成本书,但是“编写ActiveRecord规范'需要更改Rails 4强参数要求。

我添加了一个location_controller.rb来克服强参数错误

应用程序/控制器/ location_controller.rb

class LocationController < ActionController::Base

  params = ActionController::Parameters.new(latitude: lat, longitude: long)

end

应用程序/模型/ location.rb

require 'active_record'

class Location < ActiveRecord::Base

  #attr_protected :latitude, :longitude

  validates :latitude, :longitude,:presence => true,:numericality => true

  R = 3_959 # Earth's radius in miles, approx
  def near?(lat, long, mile_radius)
    raise ArgumentError unless mile_radius >= 0

    #loc = Location.new(:latitude => lat,:longitude => long)

    loc = location.new(params)

    R * haversine_distance(loc) <= mile_radius
  end

  private
  def to_radians(degrees)
    degrees * Math::PI / 180
  end

  def haversine_distance(loc)
    dist_lat = to_radians(loc.latitude - self.latitude)
    dist_long = to_radians(loc.longitude - self.longitude)
    lat1 = to_radians(self.latitude)
    lat2 = to_radians(loc.latitude)
    a = Math.sin(dist_lat/2) * Math.sin(dist_lat/2) +
        Math.sin(dist_long/2) * Math.sin(dist_long/2) *
            Math.cos(lat1) * Math.cos(lat2)
    2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
  end

end

规格/模型/ location_spec.rb

require "spec_helper"
require "rspec/its"
require "/home/conor/geo_pictures/app/models/location"

describe Location do

  let (:latitude) { 38.911268 }

  let (:longitude) { -77.444243 }

  let (:air_space) { Location.new(:latitude => 38.911268, :longitude => -77.444243) }

  describe "#initialize" do

    subject { air_space }

    its(:latitude) { should == latitude }

    its(:longitude) { should == longitude }

  end 

  describe "#near?" do
    context "when within the specified radius" do
      subject { air_space }
      it { should be_near(latitude, longitude, 1) }
    end
    context "when outside the specified radius" do
      subject { air_space }
      it { should_not be_near(latitude * 10, longitude * 10, 1) }      
    end

  end

  context "when a negative radius is used" do

    it "raising an error" do

      expect { air_space.near?(latitude, longitude, -1) }.to raise_error ArgumentError

    end

  end

end

的Gemfile

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

gem 'rspec-rails', :group => [:test, :development]

gem 'rspec-its', :group => [:test, :development]

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

我收到的错误消息是

故障:

1)位置#初始化纬度      失败/错误:let(:air_space){Location.new(:latitude =&gt; 38.911268,:longitude =&gt; -77.444243)}      ActiveRecord的:: ConnectionNotEstablished:        的ActiveRecord :: ConnectionNotEstablished      #./spec/models/location_spec.rb:11:in block (2 levels) in <top (required)>' # ./spec/models/location_spec.rb:15:in阻止(3级)&#39;      #./spec/models/location_spec.rb:17:in块中的“块(3级)”

2)位置#initize经度      失败/错误:let(:air_space){Location.new(:latitude =&gt; 38.911268,:longitude =&gt; -77.444243)}      ActiveRecord的:: ConnectionNotEstablished:        的ActiveRecord :: ConnectionNotEstablished      #./spec/models/location_spec.rb:11:in block (2 levels) in <top (required)>' # ./spec/models/location_spec.rb:15:in阻止(3级)&#39;      #./spec/models/location_spec.rb:19:in块中的“块(3级)”

3)位置#附近?在指定半径范围内      失败/错误:let(:air_space){Location.new(:latitude =&gt; 38.911268,:longitude =&gt; -77.444243)}      ActiveRecord的:: ConnectionNotEstablished:        的ActiveRecord :: ConnectionNotEstablished      #./spec/models/location_spec.rb:11:in block (2 levels) in <top (required)>' # ./spec/models/location_spec.rb:25:in阻止(4级)&#39;      #./spec/models/location_spec.rb:26:in块中的“块(4级)”

4)位置#附近?在指定半径之外时      失败/错误:let(:air_space){Location.new(:latitude =&gt; 38.911268,:longitude =&gt; -77.444243)}      ActiveRecord的:: ConnectionNotEstablished:        的ActiveRecord :: ConnectionNotEstablished      #./spec/models/location_spec.rb:11:in block (2 levels) in <top (required)>' # ./spec/models/location_spec.rb:29:in阻止(4级)&#39;      #./spec/models/location_spec.rb:30:在&#39;

中的块(4级)

5)使用负半径引发错误时的位置      失败/错误:期望{air_space.near?(纬度,经度,-1)}。到raise_error ArgumentError        预期的ArgumentError,得到#with backtrace:          #./spec/models/location_spec.rb:11:in block (2 levels) in <top (required)>' # ./spec/models/location_spec.rb:39:in阻止(4级)&#39;          #./spec/models/location_spec.rb:39:in block (3 levels) in <top (required)>' # ./spec/models/location_spec.rb:39:in阻止(3级)&#39;

以0.01039秒结束(文件加载0.80876秒) 5个例子,5个失败

失败的例子:

rspec ./spec/models/location_spec.rb:17#位置#initialize latitude rspec ./spec/models/location_spec.rb:19#位置#initialize经度 rspec ./spec/models/location_spec.rb:26#位置#附近?在指定半径范围内 rspec ./spec/models/location_spec.rb:30#位置#附近?在指定半径之外时 rspec ./spec/models/location_spec.rb:37#使用负半径引发错误时的位置

将此添加到spec / spec_helper.rb

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", 
                                       :database => ":memory:")

将错误更改为

/home/conor/geo_pictures/spec/spec_helper.rb:1:在`&#39;:未初始化的常量ActiveRecord(NameError)

1 个答案:

答案 0 :(得分:1)

您应该修复的第一个错误是:ActiveRecord::ConnectionNotEstablished。您需要在config/database.yml中配置测试数据库。

在您的规范(spec/models/location_spec.rb)中,您只需拥有require 'spec_helper'。其他类将由Rails自动加载。

最后,通常不会以该格式指定强参数。

您可以像这样指定它们:  params.require(:location).permit(:latitude, :longititude)

请参阅documentation here