我无法理解为什么我似乎无法存根这个控制器方法 :load_user,因为如果我改变实际执行的:load_user不返回和实例,我的所有测试都会失败@user.
任何人都可以看到为什么controller.stub!(:load_user).and_return(@user)当RSpec向控制器发出请求时,我的stub()似乎无法实际调用?
require 'spec_helper'
describe TasksController do
before(:each) do
@user = Factory(:user)
sign_in @user
@task = Factory(:task)
User.stub_chain(:where, :first).and_return(@user)
controller.stub!(:load_user).and_return(@user)
end
#GET Index
describe "GET Index" do
before(:each) do
@tasks = 7.times{Factory(:task, :user => @user)}
@user.stub!(:tasks).and_return(@tasks)
end
it "should should find all of the tasks owned by a user" do
@user.should_receive(:tasks).and_return(@tasks)
get :index, :user_id => @user.id
end
it "should assign all of the user's tasks to the view" do
get :index, :user_id …Run Code Online (Sandbox Code Playgroud) 我想我需要在Rails中使用STI.
这是我的班级:
class Person < ActiveRecord::Base
end
class Landlord < Person
end
Run Code Online (Sandbox Code Playgroud)
和people表有:type一个字符串列.
所以,我期望在表中看到,作为Person的每一行都将类型设置为"Person",并且每个Landlord都将类型设置为"Landlord".但是,这不是我所看到的.每个房东的类型都设置为"Landlord",但所有Person的类型都设置为nil.这很好可能是rails工作的方式,但我只是在寻找一些确认.
我的Ticket模型应用程序中有一些相当复杂的JSON响应,我希望我TicketDecorator能够构建这些响应.
但是,我已经为我的系统设置了API并使用RABL来构建那些JSON响应,所以我想重用我已经创建的模板.
我想知道是否可以在一个方法内部渲染RABL模板TicketDecorator,如下所示:
这是我的RABL模板 tickets/show.json.rabl
object @ticket
attributes :id, :link, :reported_ago_in_words_with_reporter,
:last_updated_in_words_with_updater, :priority_label, :status,
:location, :category_list
node(:attachment_urls) { |ticket| [ asset_path(ticket.first_attachment_url), asset_path(ticket.second_attachment_url), asset_path(ticket.third_attachment_url) ] }
node(:comment_count) { |ticket| ticket.comments.count }
child :recent_comments do
extends 'comments/index'
end
Run Code Online (Sandbox Code Playgroud)
这是我的TicketDecorator方法:
class TicketDecorator < ApplicationDecorator
def as_json
h.render(template: "tickets/show", formats: :json)
end
end
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为我不能成功设置@ticket,我得到一个错误说: undefined method `first_attachment_url' for nil:NilClass因为@ticket是零.
任何人都有任何好的解决方案,我可以如何使这两个很好地一起工作?
我唯一真正的想法是将模板渲染为字符串并手动使用RABL,但我不确定我是如何调用render_to_stringDraper内部的,因为它不是视图助手.
有什么想法吗?
我知道,与MSP430F2619与TI的CCSv4工作时,我可以得到一个以上的中断使用相同的中断处理程序的代码看起来是这样的:
#pragma vector=TIMERA1_VECTOR
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void){
ServiceWatchdogTimer();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我发现自己处于那个中断时,有没有办法弄清楚这些中断中的哪一个让我在这里?
这是我正在使用的一个模型,我已将其简化为最简单的形式,但我的示例仍然失败:
class User < ActiveRecord::Base
after_create :setup_lists
def setup_lists
List.create(:user_id => self.id, :name => "current")
List.create(:user_id => self.id, :name => "master")
end
end
Run Code Online (Sandbox Code Playgroud)
我想将示例规范如下:
require 'spec_helper'
describe User do
before(:each) do
@user = Factory(:user)
end
describe "#setup_lists" do
before(:each) do
List.stub(:create).with(:name => "current")
List.stub(:create).with(:name => "master")
it "creates a new master list" do
List.should_receive(:create).with(:name => "master")
end
it "creates a new current list" do
List.should_receive(:create).with(:name => "current")
end
end
end
Run Code Online (Sandbox Code Playgroud)
我期望它会工作得很好,但我留下了以下错误:
Failures:
1) User#setup_lists creates a new master list
Failure/Error: …Run Code Online (Sandbox Code Playgroud) 我有一个不能被直接访问,在传统的RESTful方式的控制,而只能通过特定的URL.
通常我已经习惯了使用GET和POST在我的控制器的规格调用控制器动作.有没有办法通过访问特定网址来运用我的控制器?
编辑:
这是我的路线:
Larzworld::Application.routes.draw do
match '/auth/:provider/callback' => 'authentications#create'
devise_for :users, :controllers => {:registrations => "registrations"}
root :to => 'pages#home'
end
Run Code Online (Sandbox Code Playgroud)
这是我的规格:
require 'spec_helper'
describe AuthenticationsController do
before(:each) do
request.env["omniauth.auth"] = {"provider" => "twitter", "uid" => "12345678"}
end
describe 'POST create' do
it "should find the Authentication using the uid and provider from omniauth" do
Authentication.should_receive(:find_by_provider_and_uid)
post 'auth/twitter/callback'
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
Failures:
1) AuthenticationsController POST create should find the Authentication using the uid and provider from omniauth
Failure/Error: …Run Code Online (Sandbox Code Playgroud) 邮政说了这一切。
我已经从github来源克隆了一个宝石(特别是辐射的)。有人告诉我如何将其安装为gem吗?
像“ gem install radiant --local”之类的东西?
我需要测试以下帮助器:
def display_all_courses
@courses = Course.all
output = ""
for course in @courses do
output << content_tag(:li, :id => course.title.gsub(" ", "-").downcase.strip) do
concat content_tag(:h1, course.title)
concat link_to("Edit", edit_course_path(course))
end
end
return output
end
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法可以测试这个输出.基本上,我只是想测试帮助器给我正确数量的li元素,也许是没有任何课程的情况.
我的第一个想法是做这样的事情:
describe DashboardHelper do
describe display_all_courses do
it "should return an list of all the courses" do
7.times{Factory(:course)
html = helper.display_all_courses
html.should have_selector(:li)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,如果我将:count选项添加到have_selector调用它突然失败,有人可以帮我弄清楚为什么会这样吗?
我有一个 NGINX 设置来代理我的应用程序服务器请求,如下所示:
daemon off;
#Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events {
use epoll;
accept_mutex on;
worker_connections 1024;
}
http {
# Instead of using Rack::Deflater and having the application serer Gzip HTML and JSON requests from
# the client, have the webserver compress them.
gzip on;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/css text/json text/javascript
application/javascript application/x-javascript application/json
application/rss+xml application/vnd.ms-fontobject application/x-font-ttf
application/xml font/opentype image/svg+xml text/xml;
underscores_in_headers on;
server_tokens off;
log_format l2met …Run Code Online (Sandbox Code Playgroud) 我有一个方法,我打电话过期?这只是为了检查自我的对象更新后是否超过24小时,如果是这样,则返回true.这是我的方法,但虽然它有效但感觉很脏,有人能想出更好的方法来实现我的目标吗?
DAY_IN_SECS = 86400
def expired?
return true unless (Time.now <=> self.updated_at + DAY_IN_SECS) == -1
end
Run Code Online (Sandbox Code Playgroud) 我们正在使用外部CMS来管理我们的网站,我想确保在我们的Rails应用程序内部,root_url指出该外部网址.我们有什么方法可以做到这一点?
就像是:
root :to => "http://wwww.example.com"
Run Code Online (Sandbox Code Playgroud) ruby ×5
rspec ×4
unit-testing ×2
c ×1
controller ×1
cors ×1
decorator ×1
draper ×1
embedded ×1
github ×1
helper ×1
http-headers ×1
inheritance ×1
interrupt ×1
model ×1
msp430 ×1
nginx ×1
rabl ×1
refactoring ×1
routing ×1
rubygems ×1
sti ×1
stub ×1