Integration test fails when implementation works
<p>I have an integration test that I can not figure out why it is not passing. I feel like my implementation works the way I want it within my controllers and all my other tests pass that are written somewhat similarly.</p>

<pre><code>integration test:

def setup
    @user = users(:hank)
    @user2 = users(:phillip)
    @item = items(:television)
  end

  test "should decrement users tokens when ticket is purchased" do
    log_in_as @user
    assert_difference "@user.tokens", -(@item.ticket_price) do
      post tickets_path, user_id: @user.id, item_id: @item.id, ticket_number: 15
    end
  end
</code></pre>

<p>^ This is the part that fails, I took out the rest to keep it clean</p>

<pre><code>tickets_controller.rb

def create
    @ticket = Ticket.new(ticket_params)
    user = current_user
    item = Item.find(params[:item_id])
    if user.tokens &gt;= item.ticket_price
      if @ticket.save
        # If I debug here and after the upd_att the functionality works. Users tokens are in fact decremented.
        user.update_attribute(:tokens, (user.tokens - item.ticket_price))
        flash[:success] = "Ticket successfully purchased."
        redirect_to item_path(@ticket.item_id)
      else
        flash[:danger] = "Ticket already purchased. Please select another."
        redirect_to item_path(@ticket.item_id)
      end
    else
      redirect_to item_path(item.id)
    end
  end
</code></pre>

<p>^ I expanded all my methods that refactored my controller to hopefully catch what is wrong. </p>

<pre><code>ticket's schema.rb

 create_table "tickets", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "item_id"
    t.integer  "ticket_number", default: 0
    t.datetime "created_at",                null: false
    t.datetime "updated_at",                null: false
  end
</code></pre>

<p>The basic functionality of the site is a user has any number of tokens that they can use to purchase tickets. When a user purchases a ticket, it should decrement the users tokens by the price of the ticket. This works the way that I want it to on the site, I just want to have a test for it to build up my suite. After hundreds of binding.pry's and searching I don't know why when it returns from the post request the user is back to having 50 tokens (the number set within the users fixture). If I put a debugger in the controller the user's token decrease to 40, but if I put a debugger after the post request before the end of the <code>assert_difference</code> it's back to 50.</p>

<p>EDIT:</p>

<p>After further testing I still can not get this to work. If I run a <code>--verbose</code> tag on the test it aborts the rake and returns </p>

<pre><code>NameError: undefined local variable or method `√' for main:Object
</code></pre>

<p>The only thing that I could think of was if I had accidentally used <code>alt</code> + <code>space</code> and added a different unicode space. I retyped out the controller to be sure and this did not resolve the issue.</p>

<p>I am thinking now that it could be my form that is causing the problems.</p>

<pre><code>show.html.haml

.purchase
  = form_tag('/tickets') do
    = hidden_field_tag 'user_id', @current_user.id if @current_user
    = hidden_field_tag 'item_id', @item.id
    = text_field_tag 'ticket_number', nil, placeholder: "Ticket Number"
    = submit_tag 'Purchase'
</code></pre>

<p>I wonder if it is the hidden_field_tags that are causing the error, or the UTF8 encoding. I am stumped.</p>