2013-09-21 31 views
0

我增加订货到我的用户模型无法保存和读取Rspec的Rails的变量文件

default_scope order: 'users.surname ASC' 

一切工作正常。

然后我想添加一个测试案例到我的spec/models/user_spec.rb文件。

不幸的是,它给出了错误。虽然有类似的测试,并且运行良好。

这里有exerpts:

require 'spec_helper' 

describe User do 

    before do 
    @user = User.new(name: 'Example', surname: 'User', email: '[email protected]', 
    password: 'foobar', password_confirmation: 'foobar') 
    end 

    subject { @user } 

    describe "remember token" do                            
    before { @user.save } 
    its(:remember_token) {should_not be_blank} 
    end 

    describe "users ordered by surname" do 
    before do 
     @user2 = User.create(name: 'Roy', surname: 'McAndy', email: '[email protected]', 
     password: 'foobar', password_confirmation: 'foobar') 

     @user3 = User.create(name: 'Roy', surname: 'Andyman', email: '[email protected]', 
     password: 'foobar', password_confirmation: 'foobar') 
    end 

    pp User.all 
    pp [@user3, @user2] 

    User.all.should == [@user3, @user2] 
    end 

    describe "with role set to admin" do 
    before do 
     @user.save! 
     @user.update_attribute(:role, "admin") 
    end 

    it { should be_admin } 
    end 

在上面Rspec的文件中,形容 “用户按姓氏排序”提供了以下错误:

bundle exec rspec 
[] 
[nil, nil] 
/home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/rspec-expectations-2.13.0/lib/rspec/expectations/fail_with.rb:32:in `fail_with': expected: [nil, nil] (RSpec::Expectations::ExpectationNotMetError) 
got: [] (using ==) 
Diff: 
@@ -1,2 +1,2 @@ 
-[nil, nil] 
+[] 

from /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/rspec-expectations-2.13.0/lib/rspec/matchers/operator_matcher.rb:56:in `fail_with_message' 
from /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/rspec-expectations-2.13.0/lib/rspec/matchers/operator_matcher.rb:94:in `__delegate_operator' 

我用漂亮的印刷(PP)为追踪目的。

很奇怪,在其他情况下user.save!工作正常。

我的错误在哪里,这里可能有什么问题?

非常感谢!

回答

1

问题在于你没有在it块内执行测试的操作,但应该是。例如:

describe 'default scope' do 
    before do 
     @user2 = User.create(name: 'Roy', surname: 'McAndy', email: '[email protected]', 
     password: 'foobar', password_confirmation: 'foobar') 

     @user3 = User.create(name: 'Roy', surname: 'Andyman', email: '[email protected]', 
     password: 'foobar', password_confirmation: 'foobar') 
    end 

    it 'should order by surname' do 
     User.all.should == [@user3, @user2] 
    end 
    end 
相关问题