0

Ok, so I'm working from a book called Agile Web Developement with Rails 4th Edition. I got up to page 83 where they have us running tests to ensure proper validation of our code up to this point. So I have checked and rechecked to make sure my code is identical to theirs. I keep getting an error. This is the test:

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

This is the error I get:

====================================

    /home/mint/Documents/depot/test/unit/product_test.rb:1:in `test': wrong number of arguments (1 for 2) (ArgumentError)
        from /home/mint/Documents/depot/test/unit/product_test.rb:1:in `<top (required)>'
        from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `require'
        from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `block in require'
        from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:236:in `load_dependency'
        from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `require'
        from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
        from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `each'
        from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `block in <main>'
        from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `select'
        from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `<main>'
    rake aborted!
    Command failed with status (1): [ruby -I"lib:test" -I"/var/lib/gems/1.9.1/gems/rake-10.0.3/lib" "/var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ]

    Tasks: TOP => test:units
    (See full trace by running task with --trace)

========================================

So I proceed to comment out one line at a time inside the test to see if this changes anything. Everything stays the same. Finaly, I change the text inside the quotes at the top of the argument. When I remove all but the double quotes, I get this:

==========================================

/home/mint/Documents/depot/test/unit/product_test.rb:1:in `test': can't convert String into Integer (TypeError)
    from /home/mint/Documents/depot/test/unit/product_test.rb:1:in `<top (required)>'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `require'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `block in require'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `require'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `each'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `block in <main>'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `select'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Command failed with status (1): [ruby -I"lib:test" -I"/var/lib/gems/1.9.1/gems/rake-10.0.3/lib" "/var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ]

Tasks: TOP => test:units
(See full trace by running task with --trace)

============================================

When I get rid of the quotes as well, I get this:

===========================================

/home/mint/Documents/depot/test/unit/product_test.rb:1:in `test': wrong number of arguments (0 for 2..3) (ArgumentError)
    from /home/mint/Documents/depot/test/unit/product_test.rb:1:in `<top (required)>'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `require'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `block in require'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/lib/ruby/vendor_ruby/active_support/dependencies.rb:251:in `require'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `each'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `block in <main>'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `select'
    from /var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Command failed with status (1): [ruby -I"lib:test" -I"/var/lib/gems/1.9.1/gems/rake-10.0.3/lib" "/var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ]

Tasks: TOP => test:units
(See full trace by running task with --trace)

=============================================

I checked the forum for this book, and so far only one person has had anything even remotely similar to my issue, but several pages forward, and no one has responded to him from way back in June of this year. So I turn to you, my friends. What do they mean "wrong number of arguments (1 for 2) and (0 for 2..3)? Apparently the only thing that affects this error report is what is between the quotes. I've tried using single quotes and got the same error. I tried replacing the text with numbers and got a different error. Help me Dani Won kenobi. You're my only hope. And thank you very veyr much. =)

2
Contributors
6
Replies
10
Views
4 Years
Discussion Span
Last Post by Dante2
0

Fixed most of this problem. turns out white space is a big deal in rails. had some other errors as well, after I fixed this one. But now I have an entirely different problem. Switched to Linux cause it moves a heck of a lot faster than windows. But now I am having problems installing sqlite3 as a native gem. I'll post more info a bit later. First going to see if I can crack the problem. Be back if I can't.

0

Fixed most of this problem. turns out white space is a big deal in rails. had some other errors as well, after I fixed this one. But now I have an entirely different problem. Switched to Linux cause it moves a heck of a lot faster than windows. But now I am having problems installing sqlite3 as a native gem. I'll post more info a bit later. First going to see if I can crack the problem. Be back if I can't.

0

I guess you should read this site to help you in writing unit tests. The test requires another module (require 'test_helper') & the test class should inherit from ActiveSupport::TestCase.

To be honest, your tests after line 3 (4+) don't seem to be that helpful. If an object of a Product is successfully created, why would you need to test for its properties/attributes? Did you verify each attribute when you create in your model? Because if it fails to create, you should get or return nil value instead.

Edited by Taywin

0

Interesting... I'm going to spend some time reading that site. I am completely new to rails so I am following along with a book. Pretty much doing what I am told and trying to understand it. The way the text was printed in the book I mistakenly believed that I was supposed to get rid of the ActiveSupport::TestCase. Took my a full day and a half to realize that that was half the problem. As for the rest... not sure why they have me writing unhelpful tests. Perhaps to show me something down the road? Idk. My latest problem is the very next test. I run the rake test:units command and this is what I get:

nathanael@nathanael-HP-Pavilion-dv7-Notebook-PC:~/Documents/depot$ rake test:units
Run options: 

# Running tests:

.F

Finished tests in 0.544049s, 3.6761 tests/s, 12.8665 assertions/s.

  1) Failure:
test_product_price_must_be_positive(ProductTest) [/home/nathanael/Documents/depot/test/unit/product_test.rb:21]:
<["Must be greater than or equal to 0.01"]> expected but was
<["must be greater than or equal to 0.01"]>.

2 tests, 7 assertions, 1 failures, 0 errors, 0 skips
rake aborted!
Command failed with status (1): [ruby -I"lib:test" -I"/var/lib/gems/1.9.1/gems/rake-10.0.3/lib" "/var/lib/gems/1.9.1/gems/rake-10.0.3/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ]

Tasks: TOP => test:units
(See full trace by running task with --trace)

And this is the series of test they want me to run:

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

test "product price must be positive" do
    product = Product.new(title: "My Book Title",
                           description: "yyy",
                           image_url: "zzz.jpg")

    product.price = -1
    assert product.invalid?
    assert_equal ["Must be greater than or equal to 0.01"],
      product.errors[:price]

    product.price = 0
    assert product.invalid?
    assert_equal ["Must be greater than or equal to 0.01"],
      product.errors[:price]

    procust.price = 1
    assert product.valid?
end

end

So why is it saying that it's wrong even though it is what it was expecting. I tried making the letters lowercase and got all kinds of error and block messages. And when I try changing the text completely, I either get a host of errors I don't understand, or I get the same one as above. Was expecting "Whatever I put here" but was "must be greater than or equal to 0.01". I don't understand why the error is the same even with changed text....

0

When you use assert_equal to compare string & string, it is case sensitive. It is similar to "==" operator (but not quite). In your case, it is the word must that doesn't match, so it fails the test. (look at your line 21 which tells the test that it should expect the same returned string by the call).

I guess the book was trying to show how things work; however, the author doesn't do a good job by giving you non-practical examples. Anyway, keep practicing and let yourself create mistake. It is a good way to learn so you will have experiences when you see something like this in the future.

Edited by Taywin

0

So I fixed the letter case, and then a buddy of mine pointed out that the other error I had was a matter of mispelling the word product. So I'm running smoothly again. Thanks a ton. =)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.