Rpsec检查记录的存在和值

iro*_*and 1 rspec ruby-on-rails

我想测试一个User模型属性的存在.所以我这样写.

describe "Authentications" do
  it "sign up with twitter" do
    visit new_user_session_path
    expect { click_link "Sign up with Twitter"}.to change(User, :count).by(1)
  end
  describe "new user" do
    let(:new_user) { User.last }
    it "should have slug" do
      #new_user its(:slug) { should eq("foo") }
      new_user.slug should exist
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

第一次测试通过,但第二次测试失败,出现这样的错误.

  1) Authentications new user should have slug
     Failure/Error: new_user.slug should exist
     NoMethodError:
       "new user" does not respond to either #exist? or #exists?
     # ./spec/features/autentication_spec.rb:13:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

我想我的方法是错误的.我该怎么用?

毕竟我不仅要检查它的存在,还要检查它的价值.但它也失败了.

Leo*_*rea 6

如果要验证值的存在,则应使用present?Rails提供的方法.

expect(new_user.slug).to be_present 应该为你想要的东西工作.