ruby-on-rails - RSpec Rails - subject + assigns as one liner?
问题描述
I am struggling with the following RSpec. Why does this works:
it 'GET articles#new creates new instance of Article' do
get :new
expect(assigns[:article]).to be_a(Article)
end
And neither of those do (found some examples with different brackets and that's why I decided to check both possibilities)
subject { get :new }
it { expect(assigns[:article]).to be_a(Article) }
it { expect(assigns(:article)).to be_a(Article) }
I am getting this error:
Failure/Error: it { expect(assigns(:article)).to be_a(Article) }
expected nil to be a kind of Article(id: integer, title: string, body: string, author_id: integer, created_at: datetime, updated_at: datetime)
# ./spec/controllers/articles_controller_spec.rb:35:in `block (4 levels) in <top (required)>'
I don't know how to retrieve "article" from the subject...
I have also tried some various combinations of "subject" and "assigns" inside it { ... }, but I couldn't get it working.
I'd rather keep it clean and store it in one line only :)
BTW: Do you have any other habits of writing Specs for controllers? (I already check for response 200)
解决方案
推荐阅读
- python - 使用 Phidgets Buttons 在 Python(PyDev) 中制作秒表程序
- javascript - 选择多个带有文本的容器并修剪前 150 个字符
- java - FileStats 类的问题
- sql - 是否可以从表格中知道上次更新的列?
- javascript - Vuetify 数据表:如何自动设置每页的行数(取决于组件填充的垂直空间)?
- linux - 当一个进程试图打开一个文件时会发生什么
- servicenow - ServiceNow API - 获取必填/必填字段和数据
- redis - 在 Redis 中建模嵌套的子集合
- image-processing - prolog中为图像处理API构建的代码应该做哪些更改
- spring-boot - 如果代理不可用,则 Spring Boot Kafka 启动