Rails3中未定义的名称方法测试错误(Undefined name method test error in Rails3)

编程入门 行业动态 更新时间:2024-10-28 18:22:20
Rails3中未定义的名称方法测试错误(Undefined name method test error in Rails3)

通过Michael Hartl的RailsTutorial进行操作并遇到以下错误 - 即使我已经将所有内容都跟随到了'T'。

1) UsersController GET 'index' for signed-in users should have an element for each user Failure/Error: response.should have_selector("li", :content => user.name) undefined method `name' for #<Array:0x000001032c07c8>

有没有其他人得到类似的错误,知道如何解决它?

我在第10章。

顺便说一句,当我尝试页面时,它会完成应该做的事情。 只是RSpec中的测试失败了。

仅供参考,这是来自users_controller_spec.rb的相关测试代码

require 'spec_helper' describe UsersController do render_views describe "GET 'index'" do describe "for non-signed-in users" do it "should deny access" do get :index response.should redirect_to(signin_path) flash[:notice].should =~ /sign in/i end end describe "for signed-in users" do before(:each) do @user = test_sign_in(Factory(:user)) second = Factory(:user, :email => "another@example.com") third = Factory(:user, :email => "another@example.net") @users = [@user, second, third] end it "should be successful" do get :index response.should be_success end it "should have the right title" do get :index response.should have_selector("title", :content => "All users") end it "should have an element for each user" do get :index @users.each do |user| response.should have_selector("li", :content => user.name) end end end end

我的spec / spec_helper.rb文件如下所示:

require 'rubygems' require 'spork' Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. ENV["RAILS_ENV"] ||= 'test' unless defined?(Rails) require File.dirname(__FILE__) + "/../config/environment" end require 'rspec/rails' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} Rspec.configure do |config| # == Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr config.mock_with :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, comment the following line or assign false # instead of true. config.use_transactional_fixtures = true ### Part of a Spork hack. See http://bit.ly/arY19y # Emulate initializer set_clear_dependencies_hook in # railties/lib/rails/application/bootstrap.rb ActiveSupport::Dependencies.clear def test_sign_in(user) controller.sign_in(user) end def integration_sign_in(user) visit signin_path fill_in :email, :with => user.email fill_in :password, :with => user.password click_button end end end Spork.each_run do end

Working through Michael Hartl's RailsTutorial and came across the following error - even though I have followed everything to the 'T'.

1) UsersController GET 'index' for signed-in users should have an element for each user Failure/Error: response.should have_selector("li", :content => user.name) undefined method `name' for #<Array:0x000001032c07c8>

Did anyone else get a similar error and know how to fix it?

I am in Chapter 10.

Btw, when I try the page it does what it is supposed to do. It's just that the test fails in RSpec.

FYI, here is the related test code from the users_controller_spec.rb

require 'spec_helper' describe UsersController do render_views describe "GET 'index'" do describe "for non-signed-in users" do it "should deny access" do get :index response.should redirect_to(signin_path) flash[:notice].should =~ /sign in/i end end describe "for signed-in users" do before(:each) do @user = test_sign_in(Factory(:user)) second = Factory(:user, :email => "another@example.com") third = Factory(:user, :email => "another@example.net") @users = [@user, second, third] end it "should be successful" do get :index response.should be_success end it "should have the right title" do get :index response.should have_selector("title", :content => "All users") end it "should have an element for each user" do get :index @users.each do |user| response.should have_selector("li", :content => user.name) end end end end

My spec/spec_helper.rb file looks like the following:

require 'rubygems' require 'spork' Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. ENV["RAILS_ENV"] ||= 'test' unless defined?(Rails) require File.dirname(__FILE__) + "/../config/environment" end require 'rspec/rails' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} Rspec.configure do |config| # == Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr config.mock_with :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, comment the following line or assign false # instead of true. config.use_transactional_fixtures = true ### Part of a Spork hack. See http://bit.ly/arY19y # Emulate initializer set_clear_dependencies_hook in # railties/lib/rails/application/bootstrap.rb ActiveSupport::Dependencies.clear def test_sign_in(user) controller.sign_in(user) end def integration_sign_in(user) visit signin_path fill_in :email, :with => user.email fill_in :password, :with => user.password click_button end end end Spork.each_run do end

最满意答案

看来你的test_sign_in方法正在返回一个数组的实例,而不是一个User对象。 您是否在test_sign_in方法中显式返回用户对象? 如果没有,看看在该方法中执行的最后一行,我感觉它的结果是一个数组。

I solved this issue, and the answer can be found on the railstutorial official forums.

更多推荐

本文发布于:2023-08-08 00:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中未   定义   错误   名称   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!