Omnipresent
Omnipresent

Reputation: 30424

undefined method ascii_compatible? for Encoding:US-ASCII

I'm trying to run a simple test\spec but getting an error. Error is only happening when the tests fail.

begin
  require 'rubygems'
  require 'test/spec'
rescue LoadError
  puts "==> you need test/spec to run tests. {sudo gem install test-spec}"
end


context "Foo" do 
  specify "should bar" do 
    "ttes".should.equal "tes"
  end
end

1) Error: test_spec {Foo} 001 should bar: NoMethodError: undefined method ascii_compatible?' for #<Encoding:US-ASCII> person.rb:11:inblock (2 levels) in '

Upvotes: 1

Views: 513

Answers (3)

dinman2022
dinman2022

Reputation: 129

My version of Ruby 1.9.1 had a gems that depended upon 'test-unit'. At least one of them forced the install of test-unit-2.4.7. For me, removing the 'test-unit-2.4.7' gem solved the problem.

In test-unit-2.4.7 we have this code:

in /home/davei/.gem/ruby/1.9.1/gems/test-unit-2.4.7/lib/test/unit/assertions.rb
   1483  
   1484            def ensure_diffable_string(string)
   1485              if string.respond_to?(:encoding) and
=> 1486                  !string.encoding.ascii_compatible?
   1487                string = string.dup.force_encoding("ASCII-8BIT")
   1488              end
   1489              string
   1490            end

Perhaps every instance of Encoding should support the 'ascii_compatible?' method, but that is not the case in Ruby 1.9.1p378. See for yourself:

(rdb:1) p string.encoding
#<Encoding:US-ASCII>
(rdb:1) p string.encoding.public_methods
[:to_s, :inspect, :name, :names, :dummy?, :_dump, :dbg, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :nil?, :===, :=~, :!~, :eql?, :class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :define_singleton_method, :hash, :__id__, :object_id, :to_enum, :enum_for, :gem, :pretty_inspect, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__]

I found 'ascii_compatible?' missing for Encoding:UTF-8 as well.

Upvotes: 0

mdenomy
mdenomy

Reputation: 333

I was getting a similar problem running Ruby 1.9.1 on Windows. My environment had been previously set up to run Ruby for some scripting of automated tests for embedded development.

I tried installing 1.9.3, but the problem was still there. Eventually uninstalled both 1.9.1 and 1.9.3, re-installed Ruby 1.9.3 and test-unit gem (2.4.5) and everything works OK now.

Upvotes: 0

Niklas B.
Niklas B.

Reputation: 95348

I can only guess, but there is something very strange going on here. Are you mixing Ruby versions or something? Are you using a gem that's not compatible with your version of Ruby? Every encoding instance should respond to ascii_compatible? in Ruby 1.9, so maybe you are using 1.8 (as you seem to be on Windows, this seems likely).

Also, a full stack trace will be of great help (you can catch the exception with begin/rescue and call backtrace on it. That way you will find out which exact line of the test-spec module fails.

Also, test-spec doesn't seem to be a very actively developed Gem. Maybe an option is to use RSpec instead, a more widely used tool with the same purpose.

Upvotes: 1

Related Questions