I have the following test for the create method on my controller
before :each do
request.env["HTTP_ACCEPT"] = 'application/json'
request.env["CONTENT_TYPE"] = "application/json"
end
test "should post create" do
params = {
trip_id: 1,
schedule: {
price: 12.50,
max_size: 1,
time: "11:00 am",
wdays: [0,1]
}
}
post :create, params
assert_response :success
end
When this hits my create method, params[:schedule][:wdays] is changed from [0,1] to ["0", "1"]
This causes my test to fail because the wdays has to be a int array. I thought about doing a .to_i in my validation but that would allow ["hello", "world"] to become [0,0].
Oddly enough, this curl command, which I thought would be doing the same thing, works just fine
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"trip_id":1, "schedule":{"price":12.50,"max_size":1,"wdays":[0,1],"time":"11:00 am"}}' http://localhost:3000/trips/1/schedules
How can I get my test to leave the array as an int array like in the curl?
If I can't get this functionality to work as is in the tests, should I change my validation to allow a string of arrays? If I do that, how should I prevent strings like "hello" from being valid?
This is my current validation:
def valid_wdays
if !wdays.is_a?(Array) || wdays.detect { |d| !(0..6).include?(d) }
errors.add(:wdays, "should be an array of ints represeting days of the week")
end
end
.to_paramon everything. github.com/rails/rails/blob/… You will have better luck with an integration test, because it actually uses ActionDispatch.wdays: [IntParam.new(0),IntParam.new(1)]-class IntParam; def initialize int; @int = int; end; def to_param; @int; end; end