如何在rspec中存根这个方法?

时间:2013-08-18 10:55:27

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

试图弄清楚这个错误:

ActionView::Template::Error:
       No route matches 
         {:controller=>"evaluations", 
          :action=>"note", 
          :student_group_id=>#  
            <StudentGroup id: 2, 
             name: "Rainbow Class", 
             user_id: 1, 
             created_at: "2013-08-18 10:45:46", 
             updated_at: "2013-08-18 10:45:46", 
             number_of_students: nil, 
             type_of_group: "Adult class (18+)">, 
           :student_id=>nil}

这在浏览器中工作正常,它只在测试中失败。

这是代码:

<%= link_to "!", eval_note_path({ student_group_id: @student_group, student_id: @student_group.students.first }) %>

和测试 - 我知道我需要在前面的块中存根,但我不知道如何让rspec做我想要的。在SO上看了看,但是顽固似乎完全取决于环境,我无法弄清楚:

describe "GET 'index" do

  before(:each) do
    @index = get :index
    @student_group.stub(:students.first).and_return(1)      
  end

  ...

  it "should have an evaluation link" do
    @index
    response.should have_selector('a', 
                                   href: eval_note_path({ 
                                         student_group_id: @student_group, 
                                         student_id: @student
                                         }),
                                   content: "!")
  end

end

1 个答案:

答案 0 :(得分:0)

尝试@student_group.id它应该有用。

it "should have an evaluation link" do
    @index
    response.should have_selector('a', 
                                   href: eval_note_path({ 
                                         student_group_id: @student_group.id, 
                                         student_id: @student
                                         }),
                                   content: "!")

由于