小编Bil*_*han的帖子

如何独立测试模型的回调方法?

我在模型中有一个方法:

class Article < ActiveRecord::Base
  def do_something
  end
end
Run Code Online (Sandbox Code Playgroud)

我还对此方法进行了单元测试:

# spec/models/article_spec.rb
describe "#do_something" do
  @article = FactoryGirl.create(:article)
  it "should work as expected" do
    @article.do_something
    expect(@article).to have_something
  end
  # ...several other examples for different cases
end
Run Code Online (Sandbox Code Playgroud)

一切都很好,直到我发现将此方法转移到after_save回调中更好:

class Article < ActiveRecord::Base
  after_save :do_something

  def do_something
  end
end
Run Code Online (Sandbox Code Playgroud)

现在我对这个方法的所有测试都破了.我必须解决它:

  • 没有更具体的调用do_something因为createsave将触发此方法,或者我将遇到重复的数据库操作.
  • 更改createbuild
  • 测试respond_to
  • 使用general model.save而不是单独的方法调用model.do_something

    describe "#do_something" do
      @article = FactoryGirl.build(:article)
      it "should work as expected" do
        expect{@article.save}.not_to raise_error …
    Run Code Online (Sandbox Code Playgroud)

activerecord rspec ruby-on-rails ruby-on-rails-3 factory-bot

34
推荐指数
2
解决办法
2万
查看次数

如何在RSpec测试中访问Sorcery?

巫术认证宝石:https://github.com/NoamB/sorcery

Sorcery的创建者在其Test :: Unit功能测试中提供了一个带有Sorcery测试助手的示例Rails应用程序:https://github.com/NoamB/sorcery-example-app/blob/master/test/functional/users_controller_test.rb

# Test::Unit functional test example
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  setup do
    @user = users(:noam)
  end

  test "should show user" do
    login_user
    get :show, :id => @user.to_param
    assert_response :success
  end
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何使用login_user我的RSpec控制器规格.

/gems/sorcery-0.7.5/lib/sorcery/test_helpers/rails.rb:7:in `login_user': 
undefined method `auto_login' for nil:NilClass (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

以下是关于上述错误的Sorcery gem中的相关代码:https: //github.com/NoamB/sorcery/blob/master/lib/sorcery/test_helpers/rails.rb

module Sorcery
  module TestHelpers
    module Rails
      # logins a user and calls all callbacks
      def login_user(user = nil)
        user ||= @user
        @controller.send(:auto_login,user)
        @controller.send(:after_login!,user,[user.send(user.sorcery_config.username_attribute_names.first),'secret']) …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails-3 sorcery

8
推荐指数
1
解决办法
4479
查看次数

在jQuery中监听文档级别的事件是否有任何缺点?

要听取元素上的事件,我想在文档层面上倾听:

$(document).on('click', '.myclass', function() {/*do something*/});
Run Code Online (Sandbox Code Playgroud)

比元素级别的风格更好:

$('.myclass').on('click', function() { /*do something*/ }); 
Run Code Online (Sandbox Code Playgroud)

原因是第一种风格也适用于动态添加的新元素.您还可以在Bootstrap中看到此样式使用了很多:https://github.com/twitter/bootstrap/blob/master/js/bootstrap-alert.js

我想广泛使用第一种风格.但是我想知道这种风格是否有任何缺点,比如性能?

jquery javascript-events

8
推荐指数
1
解决办法
5199
查看次数

android从SD卡读取图像

我正在尝试从我的SD卡读取图像,但我不确定我是否做得对.需要一些帮助

这是我阅读它的代码:

String imageInSD = "/sdcard/Hanud/" + c.getString(1) + ".PNG";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)

这是我的主要文件:

<?xml version="1.0" encoding="utf-8"?>
<WebView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

 <!-- <ImageView
    android:id="@+id/image"
    android:scaleType="center"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    />-->

    <ImageView
  android:id="@+id/imageview"
  android:layout_gravity="center"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="center"
  />


  <TextView  
    android:id="@+id/myNameText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
  />
</WebView>
Run Code Online (Sandbox Code Playgroud)

android sd-card

6
推荐指数
2
解决办法
2万
查看次数

Rails - 为什么远程提交表单后jQuery无法加载

为了简单起见,我有一个更新部分的表单.没什么特别的.在部分内容中,我有一个jQuery可排序列表.在更新表单之前,我可以毫无问题地拖动列表中的内容.

但是,在我更新列表后,就像js没有被重新加载,我必须刷新整个页面才能让事情再次滚动.

我已经尝试了一切,甚至从Ryan Bates Esq借用了演示jQuery应用程序.在这里.在我将部分js shizzle放入部分后,这也无效.

我确定这很简单,但我是一个ajax/jQuery新手,我真的很挣扎.

视图:

#test_one
  = render "test_one"
Run Code Online (Sandbox Code Playgroud)

部分"test_one"

= form_for @location, :remote => true do |f|
  %table
    %tr= collection_select(:location, :type_ids, Type.all, :id, :name, {:prompt => true}, :multiple => true, :class=>"chzn-select")
     %tr
       %td
         %ul#types.unstyled{"data-update-url" => sort_types_locations_url}
           - @types.each do |type|
             = content_tag_for :li, type do
               %span.handle
                 [Drag]
               = type.name
Run Code Online (Sandbox Code Playgroud)

update.js.erb

$("#test_one").html('<%= escape_javascript render("test_two") %>');
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

  def update
    @location = Location.find(params[:id])
    @types = @location.types.order('location_types.position').limit(2)
    respond_to do |format|
      if @location.update_attributes!(params[:location])
        flash[:notice] = 'Settings …
Run Code Online (Sandbox Code Playgroud)

jquery coffeescript ujs ruby-on-rails-3

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