Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 一个rspec角色测试失败,但几乎相同的测试成功_Ruby On Rails_Rspec_Rolify - Fatal编程技术网

Ruby on rails 一个rspec角色测试失败,但几乎相同的测试成功

Ruby on rails 一个rspec角色测试失败,但几乎相同的测试成功,ruby-on-rails,rspec,rolify,Ruby On Rails,Rspec,Rolify,我刚刚添加了gem,正在运行一些rspec测试 2项测试如下: describe "roles" do before(:each) do @user = FactoryGirl.create(:user) end it "should not approve incorrect roles" do @user.add_role :moderator @user.has_role? :admin should be_f

我刚刚添加了gem,正在运行一些rspec测试

2项测试如下:

  describe "roles" do

    before(:each) do
      @user = FactoryGirl.create(:user)
    end

    it "should not approve incorrect roles" do
      @user.add_role :moderator
      @user.has_role? :admin
      should be_false
    end

    it "should approve correct roles" do
      @user.add_role :moderator
      @user.has_role? :moderator
      should be_true
    end

  end
测试结果如下:

  1) User roles should not approve incorrect roles
     Failure/Error: should be_false
       expected: false value
            got: #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, name: nil, position: nil, location: nil, admin: false, archived: false, public_email: false, created_at: nil, updated_at: nil>
     # ./spec/models/user_spec.rb:70:in `block (3 levels) in <top (required)>'

Finished in 1.37 seconds
7 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:67 # User roles should not approve incorrect roles

Randomized with seed 13074
对于nil对象,第一个测试失败了,而第二个测试通过了,这是怎么回事

编辑


进一步检查后,
的任何测试都应该是_true
通过,而
的任何测试都应该是_false
失败,无论添加的角色是否与选中的角色匹配。

当您的测试执行时,
应该是_true
发生的情况是,应该调用被委托给主题对象(请参阅)。在这种情况下,subject对象是尚未保存到数据库的用户实例。如果您的user_spec.rb文件以
descripe user do
开头,RSpec将自动提供user.new的默认主题(请参阅)

这意味着您的测试实际上是在执行
User.new.should\u true
User.new.should\u false
。由于用户对象的计算结果始终为true,因此
应该为true
测试将始终通过(尽管可能不是出于您希望的原因),而应该为false测试将始终失败

根据您编写测试的方式,可能您的意思更像这样:

describe "roles" do

  before(:each) do
    @user = FactoryGirl.create(:user)
  end

  it "should not approve incorrect roles" do
    @user.add_role :moderator
    @user.has_role?(:admin).should be_false
  end

  it "should approve correct roles" do
    @user.add_role :moderator
    @user.has_role?(:moderator).should be_true
  end

end

当您的测试执行时,
should\u true
发生的事情是应该调用被委托给主题对象(请参阅)。在这种情况下,subject对象是尚未保存到数据库的用户实例。如果您的user_spec.rb文件以
descripe user do
开头,RSpec将自动提供user.new的默认主题(请参阅)

这意味着您的测试实际上是在执行
User.new.should\u true
User.new.should\u false
。由于用户对象的计算结果始终为true,因此
应该为true
测试将始终通过(尽管可能不是出于您希望的原因),而应该为false测试将始终失败

根据您编写测试的方式,可能您的意思更像这样:

describe "roles" do

  before(:each) do
    @user = FactoryGirl.create(:user)
  end

  it "should not approve incorrect roles" do
    @user.add_role :moderator
    @user.has_role?(:admin).should be_false
  end

  it "should approve correct roles" do
    @user.add_role :moderator
    @user.has_role?(:moderator).should be_true
  end

end

我假设示例组实际上嵌套在用
descripe User do
声明的组中,是吗

问题是每个示例的最后一行读作
should xxx
,但是
should
没有接收器,因此RSpec正在为您替换
User
的一个实例

你想要的是:

describe User do
  describe "roles" do
    before(:each) do
      @user = FactoryGirl.create(:user)
    end

    it "should not approve incorrect roles" do
      @user.add_role :moderator
      @user.has_role?(:admin).should be_false
    end

    it "should approve correct roles" do
      @user.add_role :moderator
      @user.has_role?(:moderator).should be_true
    end
  end
end
嗯,,
David

我假设示例组实际上嵌套在用
descripe User do
声明的组中,是吗

问题是每个示例的最后一行读作
should xxx
,但是
should
没有接收器,因此RSpec正在为您替换
User
的一个实例

你想要的是:

describe User do
  describe "roles" do
    before(:each) do
      @user = FactoryGirl.create(:user)
    end

    it "should not approve incorrect roles" do
      @user.add_role :moderator
      @user.has_role?(:admin).should be_false
    end

    it "should approve correct roles" do
      @user.add_role :moderator
      @user.has_role?(:moderator).should be_true
    end
  end
end
嗯,,
大卫

你确定第二名已经过去了吗?是的,数了数。尝试切换顺序,尝试在没有FactoryGirl的情况下进行操作,只使用了
用户。创建([@attr])
,所有这些操作都仍然有一个测试通过,另一个测试失败。我也尝试了,
不应该是真的
和“应该是零”,但仍然做了同样的事情。你确定第二个通过了吗?是的,数了数。尝试切换顺序,尝试在没有FactoryGirl的情况下进行操作,只使用了
用户。创建([@attr])
,所有这些操作都有一个测试通过,另一个测试失败。我也尝试了,
不应该是真的
和“应该是零”,但仍然做了同样的事情。哇……完全错过了这一点。谢谢另外,我在某处看到你对另一个问题的回答,谢谢你的回答。话虽如此,既然你们两人在同一时间用同一个答案回答了这个问题,我该如何决定选择哪一个呢?cbascom的答案比我的更详细。我会去的。哇……完全错过了。谢谢另外,我在某处看到你对另一个问题的回答,谢谢你的回答。话虽如此,既然你们两人在同一时间用同一个答案回答了这个问题,我该如何决定选择哪一个呢?cbascom的答案比我的更详细。我会和那个去的。谢谢!我完全错过了。谢谢!我完全错过了。