使用mock_model测试控制器的问题
我正在尝试测试模型,但在创建模拟模型时遇到问题。
这是控制器的一部分:
def show
@license = License.find(params[:id])
这是我的 rspec 的一部分:
it "can show a license" do
mocks = (1..3).map { mock_model(License) }
License.should_receive(:all).and_return(mocks)
post :show, {:id => 1}
但是,当我运行我的 rspec 时,我收到一个错误:
Failure/Error: License.should_receive(:all).and_return(mocks)
(<License(id: integer, license_id: string, location: string, format: string, license_type: string, language: string, context: string, licensor: string, licensee: string, resource: string, created_at: datetime, updated_at: datetime) (class)>).all(any args)
expected: 1 time
received: 0 times
另外,如果我让它工作,我如何更改许可证对象上的 id。 'mocks[0].id = 5' 有效吗?
任何帮助将不胜感激。谢谢。
I am trying to test a model but am having problems creating a mock model.
Here is part of the controller:
def show
@license = License.find(params[:id])
Here is part of my rspec:
it "can show a license" do
mocks = (1..3).map { mock_model(License) }
License.should_receive(:all).and_return(mocks)
post :show, {:id => 1}
However, when i run my rspec i get an error:
Failure/Error: License.should_receive(:all).and_return(mocks)
(<License(id: integer, license_id: string, location: string, format: string, license_type: string, language: string, context: string, licensor: string, licensee: string, resource: string, created_at: datetime, updated_at: datetime) (class)>).all(any args)
expected: 1 time
received: 0 times
Also, if I got this to work, how can I change the id of one on the License objects.
would 'mocks[0].id = 5' work?
Any help would be very appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是
License.should_receive(:find).with(1).and_return(mock)
在这种情况下,只有一个模拟就足够了:mock = mock_model(License)
Should be
License.should_receive(:find).with(1).and_return(mock)
it this case only one mock will be enough:mock = mock_model(License)