简体   繁体   English

Michael Hartl的RoR教程第11章中的Rspec方法“ be_following”

[英]Rspec method 'be_following' from Michael Hartl's RoR tutorial chapter 11 Following USers

I'm wondering where this method 'be_following' from chapter 11 came from? 我想知道第11章中的“ be_following”方法来自何处? Here's the snippet from the user_spec: 这是user_spec的片段:

describe "following" do
  it { should be_following(other_user) }
  its(:followed_users) { should include(other_user) }

  describe "followed user" do
    subject { other_user }
    its(:followers) { should include(@user) }
  end
end

I don't understand how this method is created. 我不知道如何创建此方法。 As far as I know it's not a default rspec or capybara method. 据我了解,这不是默认的rspec或capybara方法。 I'm not even sure whether or not in rspec you can use the rails generated methods from when you define model relationships such as has_many and belongs_to. 我什至不确定在rspec中是否可以使用定义模型关系(例如has_many和belongs_to)时从rails生成的方法。 Is that so? 是这样吗? And why in this case is there the method be_following when that is not even defined in the models. 以及为什么在这种情况下会在模型中甚至没有定义be_following方法的原因。 Here's the User model: 这是用户模型:

has_many :years
has_many :relationships, dependent: :destroy, foreign_key: :follower_id
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
                               class_name:  "Relationship",
                               dependent:   :destroy
has_many :followers, through: :reverse_relationships, source: :follower

def User.new_remember_token
    SecureRandom.urlsafe_base64
end

def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
end

def following?(followed)
    relationships.find_by_followed_id(followed)
end

def follow!(followed)
    relationships.create!(:followed_id => followed.id)
end

def unfollow!(other_user)
    relationships.find_by(followed_id: other_user.id).destroy!
end

RSpec has build-in dynamic matchers for methods with '?' RSpec具有用于'?'方法的内置动态匹配器 at the end. 在末尾。

Ex.: if object responds to following? 例如:对象是否响应以下内容? then you'll be able to write test like 然后您就可以编写测试

it {object.should be_following}

The same works if you need to pass parameter to the method. 如果您需要将参数传递给方法,则该方法同样有效。

Documentation: https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/predicate-matchers 说明文件: https : //www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/predicate-matchers

Some more examples: 其他示例:

class A
  def has_anything?; true end
  def has_smth?(a); !a.nil?  end
  def nice?; true end
  def as_nice_as?(x); x == :unicorn end
end

describe A do
  it {should have_anything}
  it {should have_smth(42)}
  it {should be_nice}
  it {should be_as_nice_as(:unicorn)}
  it {should_not be_as_nice_as(:crocodile)}
end

So it's be_, be_a_, be_an_ for any method ending with '?' 所以对于任何以'?'结尾的方法来说,它都是be_,be_a_,be_an_。 and have_ for has_...? 和has_代表has _...? methods. 方法。 Params are passed like: 参数的传递方式如下:

should be_like(:ruby)

Custom matchers are used for more complecated checks with ability to customize success and failure messages. 自定义匹配器用于更复杂的检查,并具有自定义成功和失败消息的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM