rspec有效吗? nil类的方法无效?

时间:2013-09-02 00:29:14

标签: ruby-on-rails rspec tdd bdd

这没有任何意义:

describe SessionsController do
  context "We should login to the system and create a session" do
    it "should create a session" do
      FactoryGirl.create(:user, :user_name => 'Sample-User', :password => '12345678910')
      post :create, :user => {:user_name => 'Sample-User', :password => '12345678910'}
      assigns(:user).valid?.should == true
    end
end

运行时出现以下错误:

Failure/Error: assigns(:user).valid?.should == true
     NoMethodError:
       undefined method `valid?' for nil:NilClass

我在其他测试中执行此类检查没有任何问题,我在文件顶部有require 'spec_helper'

这怎么不是一个有效的方法?

1 个答案:

答案 0 :(得分:0)

你查过Michael Hartl的教程吗?见Sign-in Tests