2013-08-01 10 views
5

Gần đây đã chuyển sang Rails 4 và nhận các lỗi mà tôi chưa nhận được trước đây. Tôi đang thử nghiệm một biểu mẫu mà tôi đang sử dụng Capybara/Rspec.Bắt một ActionController :: UnknownFormat, kiểm tra rspec khi tạo đối tượng mới

Khi tôi bấm vào nút trên biểu mẫu, các lỗi tôi nhận được là:

Failure/Error: find(".submit.button").click 
    ActionController::UnknownFormat: 
     ActionController::UnknownFormat 
    # ./app/controllers/office_listings_controller.rb:32:in `create' 
    # ./spec/features/office_listings_spec.rb:78:in `block (3 levels) in <top (required)>' 

Nó trỏ đến bộ điều khiển của tôi trông giống như:

def create 
    selected_amenities = params[:amenities] || [] # empty list if no amenities checked 
    @office_listing = OfficeListing.new(params[:office_listing]) 
    @office_listing.broker = current_broker 
    p current_broker 
    @office_listing.neighborhood = Neighborhood.find(params[:neighborhood_id]) 
    p @office_listing.neighborhood 
    if @office_listing && @office_listing.save! 
     @path = city_neighborhood_office_listing_path(@office_listing, :city_id => @office_listing.neighborhood.city.id, :neighborhood_id => @office_listing.neighborhood.id) 
     create_amenities(@office_listing, selected_amenities) 
     respond_to do |format| 
     format.js 
     end 
    else 
     @failure = "Unable to create office :-(" 
     respond_to do |format| 
     format.js 
     end 
    end 
    end 

và nó không giống như định dạng .js dòng mặc dù tôi có tệp create.js.erb đang được hiển thị khi tôi thực sự chạy trang.

Không hiểu tại sao trang thực sự đang chạy nhưng thử nghiệm của tôi không thành công.

Bất kỳ suy nghĩ nào sẽ được đánh giá cao !!

+4

thử nghiệm của bạn có thể chạy hành động tạo ở định dạng html trong khi trình điều khiển của bạn chỉ chạy ở định dạng js. – rmagnum2002

Trả lời

14

Tôi đã có một vấn đề tương tự:

post :duplicate, id: patient.media.first.id 

sản xuất các lỗi tương tự:

Failure/Error: post :duplicate, id: patient.media.first.id 
ActionController::UnknownFormat: 
ActionController::UnknownFormat 

tôi cố định nó bằng cách thêm định dạng cho bài gọi:

post :duplicate, id: patient.media.first.id, format: :js 
2

vâng, sử dụng định dạng:: js đã giải quyết được sự cố, trong trường hợp của tôi, mã của tôi trông giống như sau:

Trong spec của tôi:

params = { 
    profile_id: profile.id, 
    track_id: track.id, 
    format: :js 
    } 

    post :create, params 

Trong điều khiển của tôi:

respond_to do |format| 
    format.js {} 
    end 
1

Trước Rails 5:

post :action, id: ..., format: :json 

Từ Rails 5 trở đi:

post :action, params: { id: ..., format: :json } 
+0

Câu trả lời tuyệt vời mà tôi ước tôi đã thấy một giờ trước. Rails 5 FTW! – Marc