RailsTutorial第6章参数个数错误(1个为2)

时间:2015-01-26 15:27:51

标签: ruby-on-rails

    require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com")
  end


  test "should be valid" do
    assert @user.valid?
  end


  test "name should be present" do
    @user.name = ""
    assert_not @user.valid?
  end


  test "email should be present" do
    @user.email = "     "
    assert_not @user.valid?
  end


  test "name should not be too long" do
    @user.name = "a" *51
    assert_not @user.valid?
  end


  test "email should not be too long" do
    @user.email = "a" * 244 + "@example.com"
    assert_not @user.valid?
  end


 test "email validation should accept valid addresses" do
    valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
                         first.last@foo.jp alice+bob@baz.cn]
    valid_addresses.each do |valid_address|
      @user.email = valid_address
      assert @user.valid?, "#{valid_address.inspect} should be valid"
    end

  test "email validation should reject invalid addresses" do
    invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                           foo@bar_baz.com foo@bar+baz.com]
    invalid_addresses.each do |invalid_address|
      @user.email = invalid_address
      assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"  
      end 


test "email addresses should be unique" do
    duplicate_user = @user.dup
    duplicate_user.email = @user.email.upcase
    @user.save
    assert_not duplicate_user.valid?
    end

end
end
end

嗨,我是编程的新手,我正在尝试使用Rails Tutorial在线教程,通过C9.io进行编码

我坚持使用第6章(在代码清单6.27中),当我在进行$ bundle exec rake test时,我收到以下错误消息。教程说我应该通过它(绿色)。非常感谢您的协助,因为我一直在努力找出问题所在。提前谢谢!

Started

ERROR["test_email_validation_should_accept_valid_addresses", UserTest, 0.212780915]
 test_email_validation_should_accept_valid_addresses#UserTest (0.21s)
ArgumentError:         ArgumentError: wrong number of arguments (1 for 2)
            test/models/user_test.rb:47:in `test'
            test/models/user_test.rb:47:in `block in <class:UserTest>'
        test/models/user_test.rb:47:in `test'
        test/models/user_test.rb:47:in `block in <class:UserTest>'

  12/12: [=====================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.72719s
12 tests, 24 assertions, 0 failures, 1 errors, 0 skips

1 个答案:

答案 0 :(得分:5)

修复缩进后,错误源变得清晰:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com")
  end


  test "should be valid" do
    assert @user.valid?
  end


  test "name should be present" do
    @user.name = ""
    assert_not @user.valid?
  end


  test "email should be present" do
    @user.email = "     "
    assert_not @user.valid?
  end


  test "name should not be too long" do
    @user.name = "a" *51
    assert_not @user.valid?
  end


  test "email should not be too long" do
    @user.email = "a" * 244 + "@example.com"
    assert_not @user.valid?
  end


  test "email validation should accept valid addresses" do
    valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org first.last@foo.jp alice+bob@baz.cn]
    valid_addresses.each do |valid_address|
      @user.email = valid_address
      assert @user.valid?, "#{valid_address.inspect} should be valid"
    end

    test "email validation should reject invalid addresses" do
      invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                       foo@bar_baz.com foo@bar+baz.com]
      invalid_addresses.each do |invalid_address|
        @user.email = invalid_address
        assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"  
      end 


      test "email addresses should be unique" do
        duplicate_user = @user.dup
        duplicate_user.email = @user.email.upcase
        @user.save
        assert_not duplicate_user.valid?
      end

    end
  end
end

您不会关闭传递给test的块。它应该是:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com")
  end


  test "should be valid" do
    assert @user.valid?
  end


  test "name should be present" do
    @user.name = ""
    assert_not @user.valid?
  end


  test "email should be present" do
    @user.email = "     "
    assert_not @user.valid?
  end


  test "name should not be too long" do
    @user.name = "a" *51
    assert_not @user.valid?
  end


  test "email should not be too long" do
    @user.email = "a" * 244 + "@example.com"
    assert_not @user.valid?
  end


  test "email validation should accept valid addresses" do
    valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org first.last@foo.jp alice+bob@baz.cn]
    valid_addresses.each do |valid_address|
      @user.email = valid_address
      assert @user.valid?, "#{valid_address.inspect} should be valid"
    end
  end

  test "email validation should reject invalid addresses" do
    invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                       foo@bar_baz.com foo@bar+baz.com]
    invalid_addresses.each do |invalid_address|
      @user.email = invalid_address
      assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"  
    end 
  end

  test "email addresses should be unique" do
    duplicate_user = @user.dup
    duplicate_user.email = @user.email.upcase
    @user.save
    assert_not duplicate_user.valid?
  end
end