NameError:未定义的局部变量或rspec for rails 3.2.8中的方法

时间:2012-10-31 04:13:51

标签: ruby-on-rails ruby-on-rails-3 rspec

在rails 3.2.8 app中的应用程序控制器中定义了一个方法assign_user_rights。在applications_controller_spec.rb中,有一个方法的测试用例:

   it "should assign_user_right" do
      .....
      assign_user_rights
      session[:index_all_users].should be_true
    end

错误:

Failure/Error: assign_user_rights
     NameError:
       undefined local variable or method `assign_user_rights' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x57550e0>

由于assign_user_rights是在应用程序控制器中定义的,因此应该在其spec文件中很容易看到它。但事实并非如我们所见。代码有什么问题?感谢。

3 个答案:

答案 0 :(得分:1)

尝试使用controller.assign_user_rights代替

答案 1 :(得分:1)

get :assign_user_rights
response.should be_true

答案 2 :(得分:1)

请检查控制器applications_controller_spec.rb是application_controller.rb的子类。

           If it's not a sub class of application controller,  call the method using controller_name.assign_user_rights