0

I have Controller update action for my room model, where I am looking to update all other rooms that belong_to to the same user. room belongs_to user, user has_many rooms.

The spec is failing - I am less confident with Controller testing than any other testing so wanted to check whether my approach is correct. Is this the most appropriate to go about updating other associated records or is there a better way?


Controller

def update
    I18n.with_locale(params[:locale]) do
      Room.transaction do
        @room = Room.find(params[:id], lock: true)
        @room_presenter = RoomPresenter.new(@room)
        .......

        @room.update_attributes!(params.require(:room).permit!)
      end
      update_all_host_rooms_cancellation_policy(@room.cancellation_policy)
    end
    ..........
  end

  def update_all_host_rooms_cancellation_policy(cancellation_policy)
    if params[:apply_cancellation_policy_to_all].present?
      @room.user.rooms.not_in(@room).all.each do |room|
        room.update(cancellation_policy: cancellation_policy)
        admin_log(current_admin_user.login, params[:controller], params[:action], @room)
      end
    end
  end

Spec

  describe "#update_all_host_rooms_cancellation_policy(cancellation_policy)" do
    let(:host) { create(:user, :validated_host, host_badge: "certified") }
    let(:room) { create(:room, host: host) }
    let(:second_room) { create(:room, host: host) }

    it "applies the selected policy to all other properties owned by the host" do
      put :update, id: room.id, room: {cancellation_policy: "strict", apply_cancellation_policy_to_all: "1"}
      expect(room.reload.cancellation_policy).to eq "strict"
      expect(second_room.reload.cancellation_policy).to eq "strict"
    end

Associated view snippet with the checkbox

  <label class="checkbox">
    <%= check_box_tag :apply_cancellation_policy_to_all, "1", params[:apply_cancellation_policy_to_all] %>
    <%= label_tag "apply_cancellation_policy_to_all", "Apply cancellation policy to all" %>
  </label>

Finally just to explain the thinking behind the test:

I create a host with valid attributes.

let(:host) { create(:user, :validated_host, host_badge: "certified") }

Create two rooms belong to the host. Be explicit in ensuring that cancellation_policy: "standard" for clarity. As this is what I am updating.

let(:room) { create(:room, user: host, cancellation_policy: "standard") }
let(:second_room) { create(:room, user: host, cancellation_policy: "standard") }

Make the put request to call the update action - passing in the room_id, with the nested room params hash setting cancellation_policy:"strict" and apply_cancellation_policy_to_all: "1" (e.g. setting the checkbox).

it "applies the selected policy to all other properties owned by the host" do
  put :update, {id: room.id, room: {cancellation_policy: "strict", apply_cancellation_policy_to_all: "1"}}

This should have updated the cancellation_policy to strict on my first room that was passed in as the room.id. So I write my expectation for that:

  expect(room.reload.cancellation_policy).to eq "strict"

The call in the update action to #update_all_host_rooms_cancellation_policy(cancellation_policy) should be triggered and the second_room which also belongs to the Factory host should also be set to strict

  expect(second_room.reload.cancellation_policy).to eq "strict"
end

Finally the test failure which is failing on the initial room (i.e. - expect(room.reload.cancellation_policy).to eq "strict")

   expected: "strict"
   got: "standard"

The code is working in my local environment - but failing the spec - so just trying to understand this from a testing perspective.

8
  • what's the error that you're getting?
    – Taryn East
    Feb 18, 2016 at 4:04
  • 1
    Also - have you used pry or puts statements to make sure that you are executing the code you think you should be? eg def update_all_host_rooms_cancellation_policy(cancellation_policy) if params[:apply_cancellation_policy_to_all].present? puts "we have apply_cancellation_policy_to_all" room_set = @room.user.rooms.not_in(@room).all puts "trying ot apply it against rooms: #{room_set}" room_set.each do |room| puts "trying ot apply it to room: #{room}" room.update(cancellation_policy: cancellation_policy) etc etc
    – Taryn East
    Feb 18, 2016 at 4:07
  • (sorry, comment code-formatting is awful... but hopefully that gives you an idea of how you can see what's really going on when you run the spec)
    – Taryn East
    Feb 18, 2016 at 4:07
  • Thanks Taryn - will take a look at this now. I have edited my question with the rspec failure.
    – Ben Hawker
    Feb 18, 2016 at 5:57
  • 1
    right, so not much to go on. Have a go running it with the puts statements to give you an idea of what path is being executed and if it's what you expect (heh) :)
    – Taryn East
    Feb 18, 2016 at 6:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.