小编use*_*320的帖子

如何使用rspec模拟模块内的类方法

我试图测试一个调用外部API的create方法,但我在模拟外部API请求时遇到问题.继承了我的设置以及到目前为止我尝试过的内容:

class Update
  def self.create(properties)
    update = Update.new(properties)

    begin
      my_file = StoreClient::File.get(properties["id"])
      update.filename = my_file.filename
    rescue
      update.filename = ""
    end    

    update.save  
  end
end


context "Store request fails" do
  it "sets a blank filename" do
    store_double = double("StoreClient::File")
    store_double.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
    update = Update.create({ "id" => "222" })
    update.filename.should eq ""        
  end
end
Run Code Online (Sandbox Code Playgroud)

此刻我得到了这个失败

 Failure/Error: store_double.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
   (Double "StoreClient::File").get(#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x000001037a9208 @klass=Hash>)
       expected: 1 time
       received: 0 times
Run Code Online (Sandbox Code Playgroud)

为什么我的双重不工作,如何最好模拟调用StoreClient::File.get,以便我可以在成功或失败时测试创建方法?

ruby rspec mocking rspec2

4
推荐指数
1
解决办法
8225
查看次数

CodeIgniter输入过滤

我正在写一本书籍列表网站,并遇到了codeigniter的xss过滤问题.提交表单以创建列表时,包含"Javascript:"的任何标题都将替换为"[REMOVED]".我试过从POST数组访问数据,如下所示:

$title = $_POST['title'];
Run Code Online (Sandbox Code Playgroud)

避免使用Input类,但它仍然以某种方式被过滤.有没有办法解决这个问题,而不涉及关闭global_xss_filtering?

php xss codeigniter

2
推荐指数
1
解决办法
3238
查看次数

标签 统计

codeigniter ×1

mocking ×1

php ×1

rspec ×1

rspec2 ×1

ruby ×1

xss ×1