6

I have a piece of code that I am trying to run with rspec.

require 'spec_helper'

    describe "User" do
        before { @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar") }
        subject { @user }

        it { should respond_to(:name)}
    end

I get the following error

c:\Sites\sample_app>rspec spec/models/user_spec.rb
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec
/core/configuration.rb:819:in `load': c:/Sites/sample_app/spec/models/user_spec.
rb:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input (SyntaxError)
before { @user...er'

Now I retype the test in a new file and run that and no error. I guess there is some encoding issue or something that I just have not learnt about that I am not getting. Is there anyone that could shed some light on this issue?

mmcdole
  • 91,488
  • 60
  • 186
  • 222
Ryan-Neal Mes
  • 6,003
  • 7
  • 52
  • 77
  • 2
    the error is coming from line 1 where you are requiring the spec_helper, so could you share spec_helper code? – Sachin Singh Sep 22 '13 at 16:43
  • Yes as the error says "user_spec.rb:1" ... did I write up this question incorrectly? Odd it already has -1. Just looking for some help :/ – Ryan-Neal Mes Sep 22 '13 at 16:46
  • 1
    Please reread the comment from @SachinSingh :-) You're being asked to post your `spec_helper.rb` file, as that is likely the source (no pun intended) of the problem. – Peter Alfvin Sep 22 '13 at 16:54
  • Please see the file below. I am not really sure how the file has anything to do with it since it is the code above that I can retype in a new file and run rspec against it and everything works fine. – Ryan-Neal Mes Sep 22 '13 at 17:02
  • spec_helper seems fine, could you delete existing user_spec.rb and create new with this code and try? – Sachin Singh Sep 22 '13 at 17:26
  • You may have a non-printable character in your source file. You could try to show invisible charactes in your editor, maybe you'll see something special that doesn't belong there... – koffeinfrei Sep 22 '13 at 17:34
  • Yes that works as mentioned in my question, but I was just wondering why that works? These little things keep popping up and I thought it would be better to ask than to just go the easy whyout and retype the file. – Ryan-Neal Mes Sep 22 '13 at 17:39
  • @koffeinfrei ... i think it is more along the lines as you are mentioning, but struggling to do that. I used StringEncode in sublime, to expose these invisible chars, but it doesn't seem to give back anything. Any suggestions for other tools? – Ryan-Neal Mes Sep 22 '13 at 17:41
  • I've never used Sublime, but apparently there is a user file settings property: `"draw_white_space": "all"`. You could alternatively try vim with `:set list`. – koffeinfrei Sep 22 '13 at 18:01
  • That doesn't seem to work :/ I guess I will just retype it all out :/ frustrating error. Thanks all for the help! – Ryan-Neal Mes Sep 22 '13 at 18:20
  • I agree the `spec_helper` file isn't relevant, given your "if I retype the spec file, it works fine" note. Given that any additional information should be in the question anyway, I would go ahead and just delete the answer you posted. – Peter Alfvin Sep 22 '13 at 19:17

1 Answers1

19

You seem to indeed have been bitten by the "zero-width space problem". This is extremely frustrating, I know. You'd think that "show white space" should take care of the problem, particularly in an editor as advanced as Sublime.

In any event, to detect the existence of these characters in Sublime 2, you can use this plugin. to display them.

As an aside, I wasted hours if not days on this problem a few months ago when I copy/pasted some code from a website that had one of the little nasties in it.

I searched the Sublime 2 site for any postings related to this (e.g. feature requests, etc.), but didn't find anything, so I don't know if this is on their radar.

Note: The original version of this answer assumed that the zero-width space character was the same as the non-breaking space, which it is not. Non-breaking spaces, while they will still generate syntax errors, are much easier to detect since they "take up space" in your display (i.e. "look like" a space). There's also the "zero-width non-breaking space", which is different still. While it's easy to enter the non-breaking space on a Mac (i.e. Option-Space), I know of no way to enter any of the zero-width spaces.

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106