jeudi 5 février 2015

How to stop Rails' Unit Tests to automatically change date format in controller?

In my controller I have something like this:



@exam = Exam.new(params[:exam])
@exam.exam_date = DateTime.strptime(params[:exam][:exam_date], '%m/%d/%Y')
...
@exam.save


In my Unit Test, I have fixture, which has data as follows:



one:
id: 1
description: "test"
status: "Active"
exam_date: "01/01/2015"


Now while running the test



test "should create exam" do
assert_difference('Exam.count') do
post :create, exam: @exam.attributes.except("id", "created_at", "updated_at")
end
assert_equal I18n.t('string.record_was_successfully_created'), flash[:notice]
assert_redirected_to exam_url
end


I am getting error as ArgumentError: invalid date


After some inspection I have found that in Controller I get date in format Y-m-d


I have no idea where/why it gets converted to this (Y-m-d) format from m/d/Y. And how to stop getting it converted.


I have tried many other formats like



<%= Date.today.strftime("%m/%d/%Y") %>
<%= ('2015-02-05').strftime("%m/%d/%Y") %>
2015-02-05
"2015-02-05"
01/01/2015
"01/01/2015"
etc


all gets converted to Y-m-d in controller and this DateTime.strptime(params[:exam][:exam_date], '%m/%d/%Y') raises Invalid date error (It has to as it is not getting required format).


Aucun commentaire:

Enregistrer un commentaire