Ho questo esempio di esempio di controller che passa nella mia applicazione solo API basata su Rails 4.2.0 e Ruby 2.2.1Rails 5 - Esempio di specifiche controller - i parametri passati a zero ottengono il loro valore impostato su stringa vuota
let!(:params) { { user_token: user_token } }
context "- and optional address and contact details params value are received as a nil values -" do
it "doesn't set the address and contact details and responds with 201 success", check: true do
params.merge!(
address_street: nil, address_other: nil, city: nil, state: nil,
zip_code: nil, phone: nil)
post :create, params
expect(response).to have_http_status(201)
saved_client_id = json_response["id"]
saved_client = Client.find_by(id: saved_client_id)
expect(saved_client.address_street).to be_nil
expect(saved_client.address_other).to be_nil
expect(saved_client.city).to be_nil
expect(saved_client.state).to be_nil
expect(saved_client.zip_code).to be_nil
expect(saved_client.phone).to be_nil
end
end
Tuttavia valutare la mia applicazione Rails contro 5 (versione bordo) e Ruby 2.2.3 la stessa spec esito negativo con l'errore seguente:
1) Api::V1::ClientsController POST #create when receives valid client details - and optional address and contact details params value are received as nil values - doesn't set the address and contact details and responds with 201 success
Failure/Error: expect(saved_client.address_street).to be_nil
expected: nil
got: ""
# ./spec/controllers/api/v1/clients_controller_spec.rb:352:in `block (5 levels) in <top (required)>'
# ./spec/rails_helper.rb:61:in `block (3 levels) in <top (required)>'
# /home/jignesh/.rvm/gems/[email protected]/gems/database_cleaner-1.5.1/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /home/jignesh/.rvm/gems/[email protected]/gems/database_cleaner-1.5.1/lib/database_cleaner/base.rb:92:in `cleaning'
# /home/jignesh/.rvm/gems/[email protected]/gems/database_cleaner-1.5.1/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/jignesh/.rvm/gems/[email protected]/gems/database_cleaner-1.5.1/lib/database_cleaner/configuration.rb:87:in `call'
# /home/jignesh/.rvm/gems/[email protected]/gems/database_cleaner-1.5.1/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:60:in `block (2 levels) in <top (required)>'
ho ispezionato la ferrovia s codice sorgente in pochi punti e ha scoperto che i valori nil vengono trasformati in valori vuoti prima di raggiungere la logica di azione mirata del controllore.
Questo comportamento modificato sta impostando gli attributi su stringhe vuote, quando dovrebbero essere nulle.
In Gemfile di mia app (per usare Rails 5) Ho specificato Rails utilizzando seguente codice:
gem 'rails', git: 'https://github.com/rails/rails.git'
gem 'rack', :git => 'https://github.com/rack/rack.git'
gem 'arel', :git => 'https://github.com/rails/arel.git'
e in seguito Gemfile.lock può essere visto (porzioni Gem e dipendenze troncato per farla breve) :
GIT
remote: git://github.com/capistrano/rbenv.git
revision: 6f1216cfe0a6b4ac23ca4eaf8acf012e8165d247
specs:
capistrano-rbenv (2.0.3)
capistrano (~> 3.1)
sshkit (~> 1.3)
GIT
remote: https://github.com/rack/rack.git
revision: c393176b0edf3e5d06cabbb6eb9d9c7a07b2afa7
specs:
rack (2.0.0.alpha)
json
GIT
remote: https://github.com/rails/arel.git
revision: 3c429c5d86e9e2201c2a35d934ca6a8911c18e69
specs:
arel (7.0.0.alpha)
GIT
remote: https://github.com/rails/rails.git
revision: 58df2f4b4abcce0b698c2540da215a565c24cbc9
specs:
actionmailer (5.0.0.alpha)
actionpack (= 5.0.0.alpha)
actionview (= 5.0.0.alpha)
activejob (= 5.0.0.alpha)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (5.0.0.alpha)
actionview (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
rack (~> 2.x)
rack-test (~> 0.6.3)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.0.alpha)
activesupport (= 5.0.0.alpha)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.0.alpha)
activesupport (= 5.0.0.alpha)
globalid (>= 0.3.0)
activemodel (5.0.0.alpha)
activesupport (= 5.0.0.alpha)
builder (~> 3.1)
activerecord (5.0.0.alpha)
activemodel (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
arel (= 7.0.0.alpha)
activesupport (5.0.0.alpha)
concurrent-ruby (~> 1.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
method_source
minitest (~> 5.1)
tzinfo (~> 1.1)
rails (5.0.0.alpha)
actionmailer (= 5.0.0.alpha)
actionpack (= 5.0.0.alpha)
actionview (= 5.0.0.alpha)
activejob (= 5.0.0.alpha)
activemodel (= 5.0.0.alpha)
activerecord (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0.alpha)
sprockets-rails (>= 2.0.0)
railties (5.0.0.alpha)
actionpack (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
...
....
Qualcuno può, per favore, farmi sapere che cosa ha causato questo cambiamento? Immagino che abbia qualcosa a che fare con il cambiamento in Rails 5 o l'ultimo Rack. È questo un tipo di bug che deve essere corretto nella versione finale o questo è un cambiamento intenzionale.