small medium large xlarge

Generic-user-small
29 Mar 2018, 14:34
Troy Hayman (9 posts)

When i run rails test:models i get the below error

‘ProductTest#test_product_attributes_must_not_be_empty: SyntaxError: C:/Learning/Rails/AgileRails/depot/app/models/product.rb:6: syntax error, unexpected ‘:’, expecting ‘}’ with: ​​%r{​​.​​(gif|jpg|png)​​$​​}i​, ^ C:/Learning/Rails/AgileRails/depot/app/models/product.rb:6: syntax error, unexpected $undefined with: ​​%r{​​.​​(gif|jpg|png)​​$​​}i​, ^ C:/Learning/Rails/AgileRails/depot/app/models/product.rb:6: syntax error, unexpected tIDENTIFIER, expecting ‘}’ with: ​​%r{​​.​​(gif|jpg|png)​​$​​}i​, ^ C:/Learning/Rails/AgileRails/depot/app/models/product.rb:6: syntax error, unexpected tIDENTIFIER, expecting ‘}’ with: ​​%r{​​.​​(gif|jpg|png)​​$​​}i​, ^ C:/Learning/Rails/AgileRails/depot/app/models/product.rb:7: syntax error, unexpected tLABEL, expecting keyword_do or ‘{‘ or ‘(‘ ​message: ​​’must be a URL for GIF, JPG or PNG image.’

Dmfcb_pragsmall
29 Mar 2018, 14:50
David Copeland (501 posts)

Did you look at those lines of product.rb to see if you could see the syntax error?

If nothing stands out, can you post a link to your work in progress?

Generic-user-small
29 Mar 2018, 15:04
Troy Hayman (9 posts)

I don’t see anything that stands out. I even copied the file from the downloadable code. Here is a link to the project in my Drive account. Thanks. https://drive.google.com/open?id=1mmgA-QvTDgcoiFJyMgPDw0_DG7zh-uwJ

Dmfcb_pragsmall
29 Mar 2018, 15:20
David Copeland (501 posts)

Can you post your files in another way? Chrome will not let me download any of those files as they “may harm my computer” :(

Generic-user-small
29 Mar 2018, 15:47
Troy Hayman (9 posts)

Can you get it through github? https://github.com/amnesiasd/depot It’s the Product branch that i’m working out of. Sorry, new to the whole sharing code stuff. Thanks!

Generic-user-small
29 Mar 2018, 17:10
Troy Hayman (9 posts)
product_test.rb

require 'test_helper'

class ProductTest < ActiveSupport::TestCase
	test "product attributes must not be empty" do
	  product = Product.new
	  assert product.invalid?
	  assert product.errors[:title].any?
	  assert product.errors[:description].any?
	  assert product.errors[:price].any?
	  assert product.errors[:image_url].any?
	end
end

###  product.rb

class Product < ApplicationRecord
  validates :title, :description, :image_url, presence: true
  validates :title, uniqueness: true
  validates :image_url, allow_blank: true, format: {
    with:    /\.(gif|jpg|png)/\Z/,
    message: 'must be a URL for GIF, JPG or PNG image.'
  }
  validates :price, numericality: { greater_than_or_equal_to: 0.01 }

end
Generic-user-small
29 Mar 2018, 18:28
Troy Hayman (9 posts)

I get the same error when I run the test using the downloadable code as well. Weird.

Dmfcb_pragsmall
29 Mar 2018, 19:14
David Copeland (501 posts)

What version of Ruby are you using? I don’t see an obvious syntax error.

Generic-user-small
29 Mar 2018, 19:17
Troy Hayman (9 posts)
ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
Generic-user-small
30 Mar 2018, 15:30
Troy Hayman (9 posts)

Hey David, Any other thoughts you can think of? I’ve run the tests on two different Windows machines and also a linux machine and have gotten the same error. Thanks!

Dmfcb_pragsmall
31 Mar 2018, 17:01
David Copeland (501 posts)

OK, I think I figured it out and wow is it odd :)

For me, on your product branch, I get this error:

Error:
ProductsControllerTest#test_should_get_index:
NameError: undefined local variable or method `​' for #<Class:0x007f8ba37530b0>
    app/models/product.rb:7:in `<class:Product>'
    app/models/product.rb:1:in `<top (required)>'

it’s complaining about a method whose name is some sort of whitespace character. When I open up product.rb in my editor (vi), I see a bunch of strange hex characters (<200b>). My guess is that there are some odd spaces in there. If you copied from the PDF, this could happen as the way code is encoded is super weird.

Can you try going into the file and deleting all the whitespace and replacing it with regular spaces? Or, you can copy the file from this gist: https://gist.github.com/davetron5000/c19c12eb72d8c27de27641df5af6b858 which worked for me locally (there is a test failure, but the code is free of syntax errors).

Generic-user-small
02 Apr 2018, 13:34
Troy Hayman (9 posts)

Hey David, I actually started all over from scratch over the weekend and made sure i was using 2.3.3 and 5.1.3 and everything is working now. I did manually type everything in this time, so it very well could have been that I copied some of the code from the e-book (Safari Books). Thanks for your help! Very much enjoying the book thus far!

You must be logged in to comment