在RSpec的测试before_create方法和轨道3 [英] Testing before_create method in rspec and rails 3

查看:135
本文介绍了在RSpec的测试before_create方法和轨道3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究过一些tutes和所有我看到的是如何测试before_create旧帖子。此外,它好像他们都只是测试了before_create被称为即:

I've looked into some tutes and all I saw were old posts on how to test before_create. Also it seems like they're all just testing that before_create was called i.e.:

@user = User.new
@user.should_receive(:method_name_called_by_before_create)
@user.send(:before_create) (sometimes they just do @user.save)

我要创建记录后,实际测试,我的方法有效,它已分配(并保存的变量)。

I want to actually test that my method worked and that it had assigned(and saved the variables) after creating the record.

下面是我的模型:

user.rb

class User < ActiveRecord::Base
  has_one :character, :dependent => :destroy

  after_create :generate_character

  private
    def generate_character
      self.create_character(:name => "#{email}'s avatar")
    end
end

和character.rb

and character.rb

class Character < ActiveRecord::Base

  belongs_to :user

  before_create :generate_character

  private
    def generate_character
      response = api_call
      #API CALL HERE

      #set object attributes here
      self.stat1 = calculate_stat1(response) + 5
      self.stat2 = calculate_stat2(response) + 5
      self.stat3 = calculate_stat3(response) + 5            
    end

    def api_call
      return api_call_response
    end

end

我想测试产生的性格确实设置的属性没有上线,并调用API调用。使用RSpec这可能吗?我有一个JSON响应的夹具,所以我希望我可以存根出生成的字符,然后用于测试的假响应。

I want to test that generate character indeed set the attributes without going online and calling the API call. Is this possible with rspec? I have a fixture of a json response so I was hoping I can stub out generate character and then use the fake response for testing.

下面是我的character.spec:

Here's my character.spec:

describe Character do
  before(:each) do
    Character.any_instance.stub!(:api_call).and_return(fake_response.read)
    @user = Factory(:user)
    @character = @user.character
    puts  @character.inspect
end

def fake_response
  File.open("spec/fixtures/api_response.json")
end

据打印出只有5对每个人物的统计数据中。此外,我做了一个提出在character.rb的generate_character方法​​响应,它仍然打印出真正的API调用。

It prints out only 5 for each of the character's stats. Also I did a puts response in the generate_character method in character.rb and it still prints out the "real" api call.

我设法做到一个看跌期权在fake_response并且它经过那里,但它也经历了真实api_call之后,这使得存根过时。我如何通过这个?

I managed to do a puts in fake_response and it does goes through there but it also goes through the "real" api_call after, which makes the stub obsolete. How do I get through this?

推荐答案

这里的一个好方法是提取您的API调用成一个自给自足的方法。事情是这样的:

A good approach here is extracting your api call into a self contained method. Something like this:

class Character < ActiveRecord::Base
  belongs_to :user
  before_create :generate_character

  private
    def generate_character
      data = api_call

      #set object attributes from data
    end

    def api_call
      # returns a data structure
      # resulting from the call
    end
end

然后使用了RSpec的any_instance存根的api_call方法返回一个固定的数据结构

Then use RSpec's any_instance to stub the api_call method to return a fixed data structure

Character.any_instance.stub!(:api_call).and_return { {:id => 1, :attribute_one => "foo"} }
@user = User.create
@user.character.attribute_one.should == "foo"

有关any_instance检查这次提交

for more info on any_instance check this commit

这篇关于在RSpec的测试before_create方法和轨道3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆