2016-07-24 9 views
6

Uaktualniam do Rails 5, który zepsuł mój RSpec pomimo tego, że przekazuję dane, które powinienem.Jak spodziewać się skrótu Params w RSpec w Rails 5?

Problemem jest oczywiście tutaj:

expected: ({"name"=>"MyString"}) 
got: (<ActionController::Parameters {"name"=>"MyString"} permitted: true>) 

Co oznacza, że ​​muszą być w stanie rozwiązać mojego kontrolera więc twierdzenie, że spodziewa się, że ostatni. To jest linia, która wymaga zmian.

expect_any_instance_of(Hospital).to receive(:update).with({ "name" => "MyString" }) 

prawdopodobnie coś takiego

expect_any_instance_of(Hospital).to receive(:update).with(params: { "name" => "MyString" }, permitted: true) 

ja po prostu nie wiem, co składnia jest, i nie można go nigdzie znaleźć w dokumentacji Rails rozproszonego 5 lub nie istnieje Uwagi/stack Overflow pytania dotyczące rspec on Rails 5.

Pełna błąd i kontroler Spec

2) HospitalsController PUT update with valid params updates the requested hospital 
Failure/Error: if @hospital.update(hospital_params) 

    #<Hospital id: 43, name: "MyString", reference_code: "RefCod", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, contact_phone: "+61-000-000-000", website_link: "www.example.com", street_number: "01", street: "Somewhere St", suburb: "Suburb", state: "ACT", postcode: "1111", description: "MyText MyText MyText MyText MyText MyText MyText M...", areas_of_specialization: "MyText MyText MyText MyText MyText MyText MyText M...", created_at: "2016-07-24 22:28:24", updated_at: "2016-07-24 22:28:24"> received :update with unexpected arguments 
    expected: ({"name"=>"MyString"}) 
      got: (<ActionController::Parameters {"name"=>"MyString"} permitted: true>) 
    Diff: 
    @@ -1,2 +1,2 @@ 
    -[{"name"=>"MyString"}] 
    +[<ActionController::Parameters {"name"=>"MyString"} permitted: true>] 

# ./app/controllers/hospitals_controller.rb:54:in `block in update' 

Controller metoda Spec

describe "PUT update" do 
    describe "with valid params" do 
    it "updates the requested hospital" do 
     hospital = Hospital.create! valid_attributes 
     # Assuming there are no other hospitals in the database, this 
     # specifies that the Hospital created on the previous line 
     # receives the :update_attributes message with whatever params are 
     # submitted in the request. 
     expect_any_instance_of(Hospital).to receive(:update).with({ "name" => "MyString" }) 
     put :update, {:id => hospital.to_param, :hospital => { "name" => "MyString" }}, valid_session 
    end 

    it "assigns the requested hospital as @hospital" do 
     hospital = Hospital.create! valid_attributes 
     put :update, {:id => hospital.to_param, :hospital => valid_attributes}, valid_session 
     expect(assigns(:hospital)).to eq(hospital) 
    end 

    it "redirects to the hospital" do 
     hospital = Hospital.create! valid_attributes 
     put :update, {:id => hospital.to_param, :hospital => valid_attributes}, valid_session 
     expect(response).to redirect_to(hospital) 
    end 
    end 
...etc 

Odpowiedz

6

Czy próbowałeś tylko przy użyciu ActionController :: Parametry obiektu jako wartość czekasz?

na przykład:

expect_any_instance_of(Hospital).to receive(:update).with(ActionController::Parameters.new('name':'MyString'))

+4

Ty bardzo wiele, nie miał pojęcia, można po prostu połączyć się niektóre parametry, takie jak to. Oto, co zadziałało, zwróć uwagę na włączenie zezwolenia na koniec - 'expect_any_instance_of (Hospital) .to otrzymać (: update) .with (ActionController :: Parameters.new ('name': 'MyString'). Permit (: name))' – user2792268

Powiązane problemy